Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Windows Registry Files[Next]: Transfering (PIPING) Data Between Programs
\->Introduction->Return Codes (ERRORLEVEL)

Return Codes (ERRORLEVEL)

A return code is an integer value representing the state of a programs execution. Typically an error code of zero means that the program was successful and others indicate differemt levels of informational, warning or error events.

The return of a program or command typically sets the "ERRORLEVEL" environment variable.

A lot of programs return meaningful return codes while other badly written ones like Microsofts "cscript.exe" can return "0" even on complete failures caused by syntax errors!

You may need to do some testing to confirm how reliable the return codes are because of the "cscript" bug mentioned above I return my own non-zero return code to indicate success from critical VBSCRIPT code so I can reliably detect successful completion.

I have used the "dir" command twice below, once for a successful command (ERRORLEVEL=0) and a failed command (ERRORLEVEL=1):

C:\TMP>dir mvp.gif
 Volume in drive C is [C] SYSTEM
 Volume Serial Number is 7065-D79F

 Directory of C:\TMP
02/10/2004  04:01 PM             4,617 mvp.gif
               1 File(s)          4,617 bytes
               0 Dir(s)  369,788,317,696 bytes free

C:\TMP>echo RC: %ERRORLEVEL%
RC: 0

C:\TMP>dir NoSuch.gif
 Volume in drive C is [C] SYSTEM
 Volume Serial Number is 7065-D79F

 Directory of C:\TMP

File Not Found

C:\TMP>echo RC: %ERRORLEVEL%
RC: 1

The "EXIT" command can be used in batch files to return a specific return code to the caller.


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Windows Registry Files[Next]: Transfering (PIPING) Data Between Programs


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.