Batch file 在批处理文件中测试ERRORLEVEL不会';t返回预期结果

Batch file 在批处理文件中测试ERRORLEVEL不会';t返回预期结果,batch-file,Batch File,我有一个Windows7x64系统,我想让一个批处理文件执行一个程序,然后测试进程的退出代码,并根据该退出代码执行一些操作。例如: @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

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

@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%

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

非错误级别0
没有如您所想的那样工作

首先,它评估
errorlevel0

由于错误级别为0或更大,因此计算结果为
TRUE

然后它计算
而不是

NOT TRUE
FALSE
,这意味着您的第一条消息没有打印出来


第三个结果并没有反映故障,所以我不知道你的系统出了什么问题。为了检查错误级别,我将
echo ERRORLEVEL=%ERRORLEVEL%
放在每个exit语句之后

非ERRORLEVEL 0
没有按照您的想法工作

首先,它评估
errorlevel0

由于错误级别为0或更大,因此计算结果为
TRUE

然后它计算
而不是

NOT TRUE
FALSE
,这意味着您的第一条消息没有打印出来

第三个结果并没有反映故障,所以我不知道你的系统出了什么问题。为了检查错误级别,我将
echo ERRORLEVEL=%ERRORLEVEL%
放在每个exit语句之后