R. E. Cook
Contact Us 

Dr. Cook

RunningShoes Library for WinRunner 

Custom WinRunner Functions 
and Utilities,
to Your Specs

A Random Collection of WinRunner Solutions

Creating DLLs for WinRunner 
"DLLs Demystified" 
(Mercury Interactive's Worldwide Users Conference 2000)

Using a batch file to capture an executable's return value (exit code). 


WinRunner's invoke_application() swallows the executable's return value. If that return value matters you'll need a means of capturing it.

Executing via a batch file (.bat)

Some Windows programs can be launched via a batch file. The batch file can be set up to capture the return value from the program and either echo it to the screen or write it to a file. To fully automate the process, write the value to a file, then read the output file in WinRunner for the return code.

Simple sample batch file

Here's a sample that invokes the Notepad and writes its return code to an output file  named c:\out.txt. This batch file is named c:\\batchfiles\\notepad.bat:

rem Launch Notepad (using Windows NT).

rem Write the return value to a file.
echo %ERRORLEVEL% > c:\out.txt

The variable %ERRORLEVEL% captures the return code. The percent signs are part of the variable's name.

The echo statement writes that return code. Normally it will write it to the 
screen, but the > sign pipes it to the file instead.

The rem statements are just comments (remarks).

Batch file with parameters

This sample invokes a file compare program, passing the names of the two files as parameters. The return value is again written to an output file named c:\out.txt. This batch file is named c:\\batchfiles\\FileCompare.bat:

rem Launch the file compare
c:\...\FileCompare.exe %1 %2

rem Write the return value to a file.
echo %ERRORLEVEL% > c:\out.txt

The variables %1 and %2 capture the command-line arguments, in order.

Using the batch files

The batch files must be named with a .bat extension. You can launch them in WinRunner with the dos_system() statement. 

The first example needs only the path to the batch file:

# Launch Notepad via a batch file.

The second example needs the path to the batch file and the path to both comparison files. The filenames are separated with spaces (remember the DOS command line?):

# Call the file comparison batch file.
dos_system("c:\\batchfiles\\FileCompare.bat c:\\file1.txt c:\\file2.txt");   

Reading the return value in WinRunner 

The output file is a simple text file, containing a single line with the return value (an integer). Read the file using WinRunner's standard file statements. Then do whatever you want with the return value.

Note that each time you execute the batch file, the output file is overwritten. Thus if you execute the file in a loop, you must read the output file before continuing with the next iteration of the loop.

If for some reason you needed to retain the output files without overwriting them, you could pass an output filename as another parameter (named %3). Then the echo statement would be:

echo %ERRORLEVEL% > %3

The dos_system() statement in this case is left as an exercise for the reader.


Top of  This Page | R. E. Cook Home Page

Copyright R. E. Cook Consulting, 2000 - 2011.
All rights reserved.

The site is designed to look best when viewed with Internet Explorer 5.0 - 8.0. Results with other browsers will probably deviate from what is intended.

NOTE: This website refers to registered trademarks and service marks that are owned by other companies, including Mercury Interactive, WinRunner, TestDirector, TSL, Microsoft, Windows.