Arm*_*yan 34 windows process batch-file exit-code
我想用批处理文件启动一个进程,如果它返回非零,则执行其他操作.我需要正确的语法.
像这样的东西:
::x.bat
@set RetCode=My.exe
@if %retcode% is nonzero
handleError.exe
Run Code Online (Sandbox Code Playgroud)
作为奖励,您可以考虑回答以下问题,请:)
if
?My.exe
无法启动因为某些DLL丢失,我的工作是否正常?如果没有,我怎么能检测到My.exe
无法启动?Edu*_*rch 57
ERRORLEVEL
将包含最后一个命令的返回码.可悲的是,你只能检查>=
它.
errorlevel 号码
仅当Cmd.exe运行的先前程序返回等于或大于Number的退出代码时,才指定true条件.
所以要检查0你需要在盒子外思考:
IF ERRORLEVEL 1 GOTO errorHandling
REM no error here, errolevel == 0
:errorHandling
Run Code Online (Sandbox Code Playgroud)
或者,如果您想首先编写错误处理代码:
IF NOT ERRORLEVEL 1 GOTO no_error
REM errorhandling, errorlevel >= 1
:no_error
Run Code Online (Sandbox Code Playgroud)
有关BAT编程的更多信息:http://www.ericphelps.com/batch/
或更具体的Windows cmd
:使用批处理文件的MSDN
shf*_*301 16
如何用if编写复合语句?
您可以使用括号在if块中编写复合语句.第一个括号必须单独出现if和第二个行.
if %ERRORLEVEL% == 0 (
echo ErrorLevel is zero
echo A second statement
) else if %ERRORLEVEL% == 1 (
echo ErrorLevel is one
echo A second statement
) else (
echo ErrorLevel is > 1
echo A second statement
)
Run Code Online (Sandbox Code Playgroud)
这不完全是问题的答案,但每次我想知道如何让我的批处理文件退出时,我都会在这里结束,并在进程返回非零代码时出现错误代码。
所以这里是答案:
if %ERRORLEVEL% NEQ 0 exit %ERRORLEVEL%
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
102903 次 |
最近记录: |