batch-file – If statements – Comparing Errorlevel

If Errorlevel 1 (
    Echo Errorlevel is 1 or higher
    
    REM The phrase "1 or higher" is used because If Errorlevel 1 statement means:
    REM                                          If %Errorlevel% GEQ 1
    REM                                      Not If %Errorlevel% EQU 1
)

or

If "%Errorlevel%"=="1" (
    Echo Errorlevel is 1
)

The script above would check the variable Errorlevel(built-in). The not operator can be used.

Set "Test=%Errorlevel%"

If "%Test%" == "1" (
    Echo Errorlevel is 1
)

This one also works.

Please note that some commands do not affect the errorlevel:

  • Break
  • Echo
  • Endlocal
  • For
  • If
  • Pause
  • Rem
  • Rd / Rmdir
  • Set
  • Title

The following commands set but not clear errorlevel:

  • Cls
  • Goto
  • Keys
  • Popd
  • Shift

The following commands set exit codes but not the errorlevel:

  • Rd / Rmdir

The following commands set errorlevel but not the exit codes:

  • Md / Mkdir

if you want to reproduce, please indicate the source:
batch-file – If statements – Comparing Errorlevel - CodeDay