检查过程中使用批处理文件,返回0 [英] Check if process returns 0 with batch file

查看:154
本文介绍了检查过程中使用批处理文件,返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始一个批处理文件的过程,如果返回非零,做别的事情。我需要正确的语法为

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 that My.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆