批处理文件测试错误级别 [英] batch file test error level
问题描述
我正在尝试在另一个 exe 成功执行后有条件地从批处理文件中运行一个 exe.
I'm trying to conditionally run an exe from a batch file conditionally upon another exe executing successfully.
我尝试了几种不同的 IF 和 ERRORLEVEL 组合,但似乎都不起作用
I've tried a few different combinations of IF and ERRORLEVEL but none seem to work
"......TeamBuildTypesCurrent BranchDatabaseUpdate.exe" -s localhostsql2008r2
IF %ERRORLEVEL% 1(
"......TeamBuildTypesCurrent BranchDatabaseUpdate.exe" -s localhostsql2008
)
Pause
给我错误
1(此时出乎意料.
我哪里出错了?
推荐答案
IF ERRORLEVEL ...
是一种自 DOS 时代就支持的特殊语法,%ERRORLEVEL%
WinNT 中添加了变量支持.
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...
要使用变量,请使用正常的 IF 语法:if %errorlevel%==0 echo success...
To use the variable, use the normal IF syntax: if %errorlevel%==0 echo success...
请注意,如果有人执行 set errorlevel=foo
,%errorlevel%
将停止工作,并且它可能不会针对内部 cmd.exe 命令更新.
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:
这篇关于批处理文件测试错误级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!