error-handling - 批处理文件测试错误级别

标签 error-handling batch-file

我试图在另一个 exe 成功执行后有条件地从批处理文件中运行一个 exe。

我尝试了几种不同的 IF 和 ERRORLEVEL 组合,但似乎都不起作用

"..\..\..\TeamBuildTypes\Current Branch\DatabaseUpdate.exe" -s localhost\sql2008r2 

IF %ERRORLEVEL% 1(
"..\..\..\TeamBuildTypes\Current Branch\DatabaseUpdate.exe" -s localhost\sql2008
)
Pause

给我错误

1( was unexpected at this time.



我哪里出错了?

最佳答案

IF ERRORLEVEL ...是自 DOS 时代就支持的特殊语法,%ERRORLEVEL%在 WinNT 中添加了变量支持。

原始语法是这样使用的:

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

要使用该变量,请使用正常的 IF 语法:if %errorlevel%==0 echo success...
请注意 %errorlevel%如果有人这样做就停止工作 set errorlevel=foo它可能不会为内部 cmd.exe 命令更新。

另一种解决方案是使用 && :
call someapp.exe && (echo success) || (echo error!)

有(至少)两种已知的错误级别被破坏的情况,您必须使用 ||反而:
  • RD / RMDir
  • > file redirection
  • 关于error-handling - 批处理文件测试错误级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812484/

    相关文章:

    windows - 如何在批处理文件函数参数中转义&符号?

    javascript - Requirejs组合应用程序window.onerror错误处理

    java - 如何使用 bat 文件在 Windows 服务器上卸载 java?

    perl - 如何在 .pl 和 .bat 文件中暂停控制台窗口?

    forms - Access-在Access中通过表单输入重复项以显示现有记录

    windows - 如何在 Windows 关闭前运行一个 bat 文件?

    batch-file - 我该如何修复 : 'access denied' with the move command in windows 7?

    r - R “for loop”错误消息{}

    error-handling - RxJava : calling onError without finishing/unsubscribing

    error-handling - 为什么 TCL CATCH 命令不返回错误?