Foolproof way to check for nonzero (error) return code in windows batch file
- by Pat
Intro
There's a lot of advice out there for dealing with return codes in batch files (using the ERROLEVEL mechanism), e.g.
Get error code from within a batch file
ERRORLEVEL inside IF
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
)