ERRORLEVEL
ERRORLEVEL is a special value used in the Windows command line and batch scripting to capture the exit status returned by the last executed program. It is not a conventional environment variable you assign with SET; instead, the command interpreter updates ERRORLEVEL with the exit code produced by the previous command. This value is then used to control program flow in script logic, most often through conditional tests.
In batch files and CMD, the common way to test ERRORLEVEL is with the IF ERRORLEVEL n
- if errorlevel 3 echo severe error
- if errorlevel 2 echo moderate error
- if errorlevel 1 echo minor error
This descending order ensures the most specific condition fires correctly.
To check for an exact exit code, you can compare the numeric value directly using %ERRORLEVEL% (for
Common conventions associate 0 with success and nonzero values with failure or specific error conditions, but