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.
Start Recording Mode by selecting Start/Recording after launching a program in WinTask Editor.
Check An application and click OK button in the next dialog box.
In the dialog box "Launching a program, type "cmd" in the field Program and click OK.
The Dos box is now opened. If this window is full screen, resize it by typing "Alt+Enter" .
Stop Recording mode ; you can see that the generated command is just a Shell line.
Next you must manually add the statements you wish to execute in the Dos box. First, a UseWindow must be created in order to send your keystrokes to the Dos box.
Automatically generate the window name using Spy : run the script, the script ends with the Dos box opened. Click the minimized WinTask window in the taskbar to restore WinTask and select Start/Spy. Click Find Window in the Spy window. Move the pointer to the Dos window. Paste the name of this window into the script by clicking Paste. The following command is inserted into the script : "CMD.EXE|ConsoleWindowClass|C:\Windows\System32\cmd.exe".
Create the UseWindow function using the following syntax : UseWindow("CMD.EXE|ConsoleWindowClass|C:\Windows\System32\cmd.exe")
To automatize for example the Dir Dos command and then Exit, add after the UseWindow line those two lines:
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")