wrong value of %errorlevel% in nested if in .bat file

后端 未结 1 1052
醉酒成梦
醉酒成梦 2021-01-22 11:50

I have written a .bat file to first run a program, if it is correctly finished I run another program and check return value of it.

first-program.exe         


        
相关标签:
1条回答
  • 2021-01-22 12:34

    Both instances of %ERRORLEVEL% are in the same block of code and thus both get their values at the moment when the first instance is updated. Consider enabling delayed expansion of variables with enabledelayedexpansion and replacing %ERRORLEVEL% with !ERRORLEVEL! to update each instance individually. For instance:

    @echo off
    setlocal enabledelayedexpansion
    first-program.exe
    IF "!ERRORLEVEL!"=="0" (
        second-program.exe
        IF "!ERRORLEVEL!"=="0" (
            ECHO OK
        ) ELSE (
            ECHO NOK
        )
    )
    endlocal
    
    0 讨论(0)
提交回复
热议问题