ERRORLEVEL inside IF

后端 未结 2 1796
你的背包
你的背包 2020-12-01 13:34

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

相关标签:
2条回答
  • 2020-12-01 14:11

    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
    
    )
    
    0 讨论(0)
  • 2020-12-01 14:14

    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!
    )
    
    0 讨论(0)
提交回复
热议问题