How to automate a Dos Box

WinTask can automate tasks in a Windows Dos Box.

There are limitations when using a Dos box which makes creating scripts more difficult : the Recording Mode will not record the keystrokes you enter in a Dos box. This means you must create your scripts manually.

In the following example, we demonstrate how to create a script to launch a Dos box, type the Dir command and exit the Dos box with the Exit command.

SendKeys("Dir<Enter>")
SendKeys("Exit<Enter>")

The script is complete. Before launching it, close the still-opened Dos box (type "Exit" in the Dos box and press Enter).

You can automate multiple Dos boxes :

#ActionTimeout=5

Shell("cmd.exe",1)
Usewindow("CMD.EXE|ConsoleWindowClass|C:\WINDOWS\System32\cmd.exe",1)

sendkeys("dir<Enter>")

Shell("cmd.exe",1)
Usewindow("CMD.EXE|ConsoleWindowClass|C:\WINDOWS\System32\cmd.exe",2)

sendkeys("hello")

Three other scripts which can help for Dos box automation :

How to select and replay menu selections in a Dos application, we show how to do it for EDIT application, option File/Save As :

sub Save_As(file_name$)

usewindow(fen_edit$)

sendkeys("<Alt f>")

pause 5 ticks

sendkeys("a")

pause 1

sendkeys(file_name$)

pause 5 ticks

sendkeys("<Enter>")

pause 1

sendkeys("<Alt f>")

pause 5 ticks

sendkeys("x")

endsub

 

 

' The window name of the Edit application is different depending on Windows versions.
' Below is the name under Windows Vista
fen_edit$="CMD.EXE|ConsoleWindowClass|C:\Windows\system32\cmd.exe - edit"

save_as("c:\my_file.txt")

How to automate the system menu in the Dox box: a Select all and a Copy.

win$="CMD.EXE|ConsoleWindowClass|C:\WINDOWS\system32\cmd.exe"

UseWindow(win$)

#ActionTimeout=15

 

'Send Alt+blank for opening System menu - note the 2 spaces after the Alt.

SendKeys("<Alt >",NoActivate)

 

'Send E for Edit option

SendKeys("E",NoActivate)

pause 5 ticks

 

'Send S for Select all

SendKeys("s",NoActivate)

pause 5 ticks

 

'Send enter for Copy

SendKeys("<Enter>",NoActivate)

pause 5 ticks

 

'it's now in Clipboard

msgbox(getclipboard$())

 

How to wait for a key in a Dos box, and so next actions are replayed only when the specified key has been pressed :

Shell("Notepad.exe")

Shell("cmd",1)

UseWindow("CMD.EXE|ConsoleWindowClass|C:\WINDOWS\system32\cmd.exe")

'Here all the sendkeys you want in the Dos box, for instance

sendkeys("<enter>")

pause 5 ticks

sendkeys("Dir <enter>")

 

'Wait for F12 ; a wait for a key in a Dos box works only if

' the Dos box has not the focus

'So, as a trick, we generate a new UseWindow on a Window present on the desktop

'The Dos box has not the focus but the user still sees it

UseWindow("EXPLORER.EXE|Button")

 

Pause until

Key("<F12>")

PauseFalse

MsgBox("'Wait for' at line " + #ErrorLine$ + " has failed !",16,"Runtime error")

End

EndPause

 

'switch to Notepad

UseWindow("NOTEPAD.EXE|Edit|Untitled - NotePad|1",1)

sendkeys("hello")