It's highly unrecommendable to set the value of errorlevel
manually so in this context it is not exactly a variable. To set the errorlevel on exiting the batch file use e.g. exit /b 5
(or exit 5
which will exit the whole cmd session). If you set it manually it will overwrite the 'real' value and this can cause some errors.
To check the value of errorlevel you can use:
if %errorlevel% == 5 (
echo some command
)
Or
if errorlevel 5 (
echo some command
)
Though in the second case it will enter the if condition also if the value of errorlevel is bigger than 5. The good thing about the second approach is that it will work also when the %errorlevel%
variable is overwritten.
The command prompt also sets the variables %=ExitCode%
which will store the errorlevel converted to hex. And its more difficult to be overwritten as it starts with =
. Also %=ExitCodeAscii%
wich will store the errorlevel as asci i character if the errorlevel is bigger than 32.