Foolproof way to check for nonzero (error) return code in windows batch file

Posted by Pat on Stack Overflow See other posts from Stack Overflow or by Pat
Published on 2012-06-07T16:13:09Z Indexed on 2012/06/07 16:40 UTC
Read the original article Hit count: 237

Filed under:
|

Intro

There's a lot of advice out there for dealing with return codes in batch files (using the ERROLEVEL mechanism), e.g.

Some of the advice is to do if errorlevel 1 goto somethingbad, while others recommend using the %ERRORLEVEL% variable and using ==, EQU, LSS, etc. There seem to be issues within IF statements and such, so then delayedexpansion is encouraged, but it seems to come with quirks of its own.

Question

What is a foolproof (i.e. robust, so it will work on nearly any system with nearly any return code) way to know if a bad (nonzero) code has been returned?

My attempt

For basic usage, the following seems to work ok to catch any nonzero return code:

if not errorlevel 0 (
    echo error level was nonzero
)

© Stack Overflow or respective owner

Related posts about Windows

Related posts about batch