如何让我在遇到一个错误的批处理文件终止? [英] How do I make a batch file terminate upon encountering an error?
问题描述
我有一个与不同的参数,一遍又一遍地调用同一个可执行的批处理文件。我如何让它立即终止,如果其中一个呼叫返回任何级别的错误code?
I have a batch file that's calling the same executable over and over with different parameters. How do I make it terminate immediately if one of the calls returns an error code of any level?
基本上,我想等效的MSBuild的 ContinueOnError = FALSE
。
Basically, I want the equivalent of MSBuild's ContinueOnError=false
.
推荐答案
检查错误级别
在如果
语句,然后退出/ b
(退出在乙 ATCH文件只,而不是整个过程的cmd.exe),除0以外的值。
Check the errorlevel
in an if
statement, and then exit /b
(exit the batch file only, not the entire cmd.exe process) for values other than 0.
same-executable-over-and-over.exe /with different "parameters"
if %errorlevel% neq 0 exit /b %errorlevel%
如果你想ERRORLEVEL的价值传播您的批处理文件外
If you want the value of the errorlevel to propagate outside of your batch file
if %errorlevel% neq 0 exit /b %errorlevel%
但如果这是在为
它变得有点棘手。你需要的东西更像是:
but if this is inside a for
it gets a bit tricky. You'll need something more like:
setlocal enabledelayedexpansion
for %%f in (C:\Windows\*) do (
same-executable-over-and-over.exe /with different "parameters"
if !errorlevel! neq 0 exit /b !errorlevel!
)
编辑:您必须在每个命令后检查错误。有没有全球错误转到类型的构造输入cmd.exe / command.com批次。我也更新了我的code每 codeMonkey ,虽然我从来没有遇到过在负ERRORLEVEL任我批黑客在XP或Vista。
You have to check the error after each command. There's no global "on error goto" type of construct in cmd.exe/command.com batch. I've also updated my code per CodeMonkey, although I've never encountered a negative errorlevel in any of my batch-hacking on XP or Vista.
这篇关于如何让我在遇到一个错误的批处理文件终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!