batch file test error level

后端 未结 2 1516
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 00:19

I\'m trying to conditionally run an exe from a batch file conditionally upon another exe executing successfully.

I\'ve tried a few different combinations of IF and E

相关标签:
2条回答
  • 2020-12-06 00:40

    Negative errorlevels can create problem. Try something like this:

    IF '%ERRORLEVEL%'=='0' GOTO OK
    
    0 讨论(0)
  • 2020-12-06 00:49

    IF ERRORLEVEL ... is a special syntax supported since the DOS days, the %ERRORLEVEL% variable support was added in WinNT.

    The original syntax is used like this:

    call someapp.exe
    if errorlevel 1 goto handleerror1orhigher
    echo succuess... 
    

    To use the variable, use the normal IF syntax: if %errorlevel%==0 echo success...

    Note that %errorlevel% stops working if someone does set errorlevel=foo and it might not get updated for internal cmd.exe commands.

    An alternative solution is to use &&:

    call someapp.exe && (echo success) || (echo error!)
    

    There are (at least) two known cases where errorlevel is broken and you must use || instead:

    • RD/RMDir
    • > file redirection
    0 讨论(0)
提交回复
热议问题