0

我有一个 Windows 7 x64 系统,我想让一个批处理文件执行一个程序,然后测试进程的退出代码并根据该退出代码执行一些操作。例如:

@rem sets the process exit code to 1. should print "success!" actually prints nothing
cmd /c exit 1
if NOT ERRORLEVEL 0 echo success!

@rem prints "success!" as expected
cmd /c exit 1
if errorlevel 1 echo success!

@rem prints "failure!" even though the exit code is 0
cmd /c exit 0
if errorlevel 1 echo failure!    

@rem prints "ERRORLEVEL=1" as expected
cmd /c exit 1
echo ERRORLEVEL=%ERRORLEVEL%

我需要做什么才能从批处理文件中获得预期的行为?

4

1 回答 1

3

NOT ERRORLEVEL 0不像你想象的那样工作

首先,它评估ERRORLEVEL 0
由于错误级别为0 或更大,因此评估为TRUE

然后它评估NOT
NOT TRUEis FALSE,这意味着您的第一条消息不会被打印。

第三个结果没有回应失败,所以我不知道你的系统出了什么问题。为了检查错误级别,我echo ERRORLEVEL=%ERRORLEVEL%在每个退出语句之后

于 2012-09-13T18:05:54.903 回答