检查过程中使用批处理文件,返回0 [英] Check if process returns 0 with batch file
问题描述
我要开始一个批处理文件的过程,如果返回非零,做别的事情。我需要正确的语法为
I want to start a process with a batch file and if it returns nonzero, do something else. I need the correct syntax for that.
事情是这样的:
::x.bat
@set RetCode=My.exe
@if %retcode% is nonzero
handleError.exe
作为奖励,你可以考虑回答下列问题,请:)
As a bonus, you may consider answering the following questions, please :)
- 如果如何编写与
复合语句
? - 如果该应用程序
My.exe
未能启动,因为某些DLL文件丢失将我的工作是否?如果没有,我怎么可以检测到My.exe
未能启动?
- How to write a compound statement with
if
? - If the application
My.exe
fails to start because some DLL is missing will my if work? If not, how can I detect thatMy.exe
failed to start?
推荐答案
ERRORLEVEL
将包含的最后一个命令的返回code。可悲的是,你只能检查> =
它。
ERRORLEVEL
will contain the return code of the last command. Sadly you can only check >=
for it.
请注意具体该行的 MSDN文档中如果
语句:
Note specifically this line in the MSDN documentation for the If
statement:
错误级别的编号的
errorlevel Number
指定一个真正的
条件只有previous程序
在CMD.EXE运行返回退出code
等于或大于编号的
所以要检查0您需要的条条框框:
So to check for 0 you need to think outside the box:
IF ERRORLEVEL 1 GOTO errorHandling
REM no error here, errolevel == 0
:errorHandling
或者,如果你想code错误处理第一:
Or if you want to code error handling first:
IF NOT ERRORLEVEL 1 GOTO no_error
REM errorhandling, errorlevel >= 1
:no_error
有关BAT编程的更多信息: http://www.ericphelps.com/batch/
或多个特定的Windows CMD
: MSDN使用批处理文件
这篇关于检查过程中使用批处理文件,返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!