MAKEMSI quickly and reliably creates MSI files in a non-programmatic way
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
[Bottom][Contents][Prev]: Maths[Next]: Read and Process a File line by line
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
\->Tips and Tricks->Batch Files->Batch File Tips and Tricks->Meaningful Return Code

Meaningful Return Code

At times you may need to worry about the return code of a batch program as some wrapping process needs to determine successful completion, for example you may wish to do this if you are running a batch file via a command processor invoked with the "ExeCa" command. Generally a return code of 0 is used to indicate success.

Method 1 - Use Always Work/Fail Commands

This method is useful where you only have two states (success and failure), a return code of zero indicates success and anything else indicates failure:

goto ExitRcError
...
:ExitRc0
    set SetRc=echo GOOD (this will always work)
    goto ExitWithRc

:ExitRcError
    set SetRc=.\NoSuchExeToSetRc.EXE (this will never work)
    goto ExitWithRc

:ExitWithRc
    %SetRc% >nul 2>&1

The code above runs one of two commands, one which will always work and so generate a return code of zero or one which can never succeed and so always returns a non-zero RC.

There must be no other statements (even a "REM") after the last line or the return code will be changed (idiots, they're everywhere...)!

Method 2 - EXIT command

The exit exit command has some parameters. I'm not sure which operating system first got them, so you may want to be wary if you need to support older operating systems. This approach has another major advantage if your invoking process needs to support other return conditions (for example success, failure and warning) as specific return codes can be set.

goto ExitRc123
...
:ExitRc0
    @rem exit /B 0
    exit 0

:ExitRc123
    @rem exit /B 123
    exit 123

The command processor (CMD.EXE) doesn't pick up return codes from an exit with "/B" (idiots, they're everywhere...)! So you should drop the "/B" to exit the command processor instead (of the batch file).


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]: Maths[Next]: Read and Process a File line by line


MAKEMSI© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Saturday May 28 2022 at 3:11pm
Visit MAKEMSI'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.