42

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 executed inside if statements don't set the %ERRORLEVEL% variable. The ERRORLEVEL (as in IF ERRORLEVEL 1, which is different from IF %ERRORLEVEL% EQU 1 ) check seems to still work fine though, so I can probably work around it, but it would still be nice to be able to print the error level. For debugging or whatever.

@echo off
Set TESTVAR=1

tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%

ThisWillSetErrorLevelTo9009ieNotRecognizedCommand

tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF %ERRORLEVEL%

ThisWillSetErrorLevelTo9009ieNotRecognizedCommand

IF %TESTVAR% EQU 1 (
    Set ERRORLEVEL=
    tasklist | find /I "IsntRunning.exe" > NUL
    echo INSIDE_IF  ERRORLEVEL %ERRORLEVEL%

    IF ERRORLEVEL 1 (
        echo INSIDE_IF2  ERRORLEVEL GREQ 1 %ERRORLEVEL%
    )
    IF ERRORLEVEL 2 (
        echo INSIDE_IF2  ERRORLEVEL GREQ 2 %ERRORLEVEL%
    )
    IF ERRORLEVEL 3 (
        echo INSIDE_IF2  ERRORLEVEL GREQ 3 %ERRORLEVEL%
    )
)

tasklist | find /I "IsntRunning.exe" > NUL
echo OUTSIDE_IF ERRORLEVEL %ERRORLEVEL%

@echo on

Putting that in a batch file and running it produces this output:

C:\Users\username\Documents\work>test.bat
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' is not recognized as an internal or external command, operable program or batch file.
OUTSIDE_IF 1
'ThisWillSetErrorLevelTo9009ieNotRecognizedCommand' is not recognized as an internal or external command, operable program or batch file.
INSIDE_IF ERRORLEVEL 9009
INSIDE_IF2 ERRORLEVEL GREQ 1 9009
OUTSIDE_IF ERRORLEVEL 1

Relevant articles:

T.S.
  • 18,195
  • 11
  • 58
  • 78
Srekel
  • 2,183
  • 3
  • 21
  • 26

2 Answers2

51

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!
)
Jim Davis
  • 5,241
  • 1
  • 26
  • 22
  • 7
    Wow, thanks. That works and even explains it. For reference, some links I found about it: http://batcheero.blogspot.com/2007/06/how-to-enabledelayedexpansion.html | http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/setlocal.mspx?mfr=true – Srekel Dec 06 '10 at 16:04
  • 6
    It's moments like this that make you understand why linuxers despise windows. You wouldn't believe how much time I wasted on that deceptively simple little bit. – Przemek D Mar 02 '19 at 16:02
1

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

)
npocmaka
  • 55,367
  • 18
  • 148
  • 187