Executing a command-line application using the Run Action

This month's Task Of The Month compliments our Tech Tech article by providing an example of executing a command-line application using the Run action that directs its output and return code to two different files. These files are then read into AutoMate variables for display and analysis.

<AMVARIABLE NAME="DirectoryContents" DESCRIPTION="Stores the contents of the directory"></AMVARIABLE>
<AMVARIABLE NAME="CommandLineResultCode" DESCRIPTION="The result code of our command line"></AMVARIABLE>

<!--- Run our command line that outputs the directory to a file called "DirListing.txt" and the error code to "ResultCode.txt". These will be written into the Temp directory (as defined by the system). Note we haev to double up the percentages around ERRORLEVEL to escape them. Otherwise AutoMate will think we are referring to an AutoMate variable called Errorlevel. --->

<AMRUN FILE="cmd.exe /C dir C:\ &gt; %TEMP%\DirListing.txt &amp;&amp; echo %%ERRORLEVEL%% &gt; %TEMP%\ResultCode.txt&quot;" WAITFORINPUT="NO" WAITFOREND="YES">
<!--- Read the results into our variables --->
<AMFILEREAD FILE="%TEMP%\DirListing.txt" RESULTVARIABLE="DirectoryContents">
<AMFILEREAD FILE="%TEMP%\ResultCode.txt" RESULTVARIABLE="CommandLineResultCode">
<!--- Clean up our files... --->
<AMFILEDELETE SOURCE="%TEMP%\DirListing.txt">
<AMFILEDELETE SOURCE="%TEMP%\ResultCode.txt">
<!--- Display the result --->
<AMMESSAGEBOX>Directory contents are:
%DirectoryContents%

Result code for the command was:

%CommandLineResultCode%</AMMESSAGEBOX>
<AMIF EXPRESSION="%CommandLineResultCode% = 0">
<AMMESSAGEBOX>The command-line executed successfully.</AMMESSAGEBOX>
<AMELSE>
<AMMESSAGEBOX>The command-line failed with return code %CommandLineResultCode%.</AMMESSAGEBOX>
</AMIF>