Just stumbled into a weird thing with %ERRORLEVEL%
and wanted to see if anyone knows why and if there\'s a way to fix it. Essentially, it seems as if commands e
if errorlevel
works without delayed expansion but works in manner similar to
if %errorlevel% <= Some_Value ...
:
@echo off
::sets errorlevel to 0
(call )
if "1" == "1" (
rem sets errorlevel to 5
cmd /c exit 5
if errorlevel 4 echo this will be printed
if errorlevel 5 echo this will be printed
rem :::: you can use this ::::::::::::::
if errorlevel 5 if not errorlevel 6 echo this will be printed ONLY when the errorlevel is 5
rem :::::::::::::::::::::::::::::::::::::
if errorlevel 6 echo this will not be printed
)
Try using setlocal enabledelayedexpansion
at the start of your batch file, and !ERRORLEVEL!
inside your IF
. This seems to work for me:
@echo off
setlocal enabledelayedexpansion
dir nul
echo %ERRORLEVEL%
if .1.==.1. (
urklbkrlksdj - not a command
echo %ERRORLEVEL%
echo !ERRORLEVEL!
)