Running Command-line Applications

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.

Related Topics

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 the following as the "Run" parameter:

cmd.exe dir > C:\Temp\MyFileListing

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.