Executing Command-line Applications Using The Run Action

by Scott Robinet, in Tech Talk, posted 1/17/07

The AutoMate Run action is used to run Windows applications by specifying the application name (and the full path to the application if necessary). For example, to run notepad, one would simply place the word "notepad.exe" into the Run parameter and notepad will start. Most applications that are designed to run on the command-line will also execute this way provided the command-line does not contain any symbols that are used primarily in a DOS box. In other words, attempting to use the Run action to execute a command-line where the output is to be written to a file, or piping together multiple commands on the same command-line, require special handling.

The reason for this is that AutoMate assumes that applications being run through the Run action are to be executed in the context of Windows and not a DOS prompt. In order to pipe or redirect output, use the cmd.exe program and supply the command-line as cmd.exe's parameters.

For example, to output the directory listing of the C:\ drive to the file C:\Temp\MyFileListing, add a new Run step to the task and specify:

cmd.exe dir > C:\Temp\MyFileListing

as the "Run" parameter. The contents of the file can then be read into an AutoMate variable using the Read From File action.

Because AutoMate is actually running the cmd.exe application and passing the command-line parameters to it, all the command line switches of cmd.exe are available to modify its runtime behavior. For example, the above example causes a visual DOS box to appear. This can be suppressed by using the /C parameter:

cmd.exe /C dir > C:\Temp\MyFileListing

This parameter causes the cmd.exe session to end once the command is completed. Using this parameter together with the "Wait until application is finished" parameter of the Run action will pause the AutoMate task execution until the command-line application has completed.

Using this method, you can group different commands onto the same line. For example, to execute the same example but also output the command result to another file:

cmd.exe /C dir > C:\Temp\MyFileListing && echo %ERRORLEVEL% > C:\Error.log

Notice that the percentages surrounding the environment variable ERRORLEVEL are doubled (or escaped) so AutoMate doesn't confused the cmd.exe's ERRORLEVEL parameter with an AutoMate variable of the same name. As before, reading Error.log into an AutoMate variable using the Read From File action can allow task execution to handle errors in the command line prompt.

This month's "Task Of The Month" ties everything together into a sample task that demonstrates the concepts covered in this article.

Happy AutoMating!