windows 检查进程是否使用批处理文件返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4451013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:53:11  来源:igfitidea点击:

Check if process returns 0 with batch file

windowsprocessbatch-fileexit-code

提问by Armen Tsirunyan

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.

我想用批处理文件启动一个进程,如果它返回非零值,请执行其他操作。我需要正确的语法。

Something like this:

像这样的东西:

::x.bat

@set RetCode=My.exe
@if %retcode% is nonzero
   handleError.exe

As a bonus, you may consider answering the following questions, please :)

作为奖励,您可以考虑回答以下问题,请:)

  • How to write a compound statement with if?
  • If the application My.exefails to start because some DLL is missing will my if work? If not, how can I detect that My.exefailed to start?
  • 复合语句怎么写if
  • 如果应用程序My.exe因为缺少某些 DLL 而无法启动,那么我的 if 会起作用吗?如果没有,我怎么能检测到My.exe无法启动?

回答by Eduard Wirch

ERRORLEVELwill contain the return code of the last command. Sadly you can only check >=for it.

ERRORLEVEL将包含最后一个命令的返回码。遗憾的是,您只能检查>=它。

Note specifically this line in the MSDN documentation for the Ifstatement:

特别注意MSDN 文档中该If语句的这一行:

errorlevel Number

Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than Number.

错误级别

仅当 Cmd.exe 运行的前一个程序返回等于或大于Number的退出代码时才指定真条件。

So to check for 0 you need to think outside the box:

因此,要检查 0,您需要跳出框框思考:

IF ERRORLEVEL 1 GOTO errorHandling
REM no error here, errolevel == 0
:errorHandling

Or if you want to code error handling first:

或者,如果您想先编写错误处理代码:

IF NOT ERRORLEVEL 1 GOTO no_error
REM errorhandling, errorlevel >= 1
:no_error

Further information about BAT programming: http://www.ericphelps.com/batch/Or more specific for Windows cmd: MSDN using batch files

有关 BAT 编程的更多信息:http: //www.ericphelps.com/batch/或者更具体的 Windows cmdMSDN using batch files

回答by shf301

How to write a compound statement with if?

如何用if写复合语句?

You can write a compound statement in an if block using parenthesis. The first parenthesis must come on the line with the if and the second on a line by itself.

您可以使用括号在 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
)

回答by Merky

The project I'm working on, we do something like this. We use the errorlevelkeyword so it kind of looks like:

我正在做的项目,我们做这样的事情。我们使用errorlevel关键字,所以它看起来像:

call myExe.exe
if errorlevel 1 (
  goto build_fail
)

That seems to work for us. Note that you can put in multiple commands in the parens like an echo or whatever. Also note that build_fail is defined as:

这似乎对我们有用。请注意,您可以在括号中放入多个命令,例如 echo 或其他任何命令。另请注意, build_fail 定义为:

:build_fail
echo ********** BUILD FAILURE **********
exit /b 1

回答by Techniquab

This is not exactly the answer to the question, but I end up here every time I want to find out how to get my batch file to exit with and error code when a process returns an nonzero code.

这不完全是问题的答案,但每次我想知道如何让我的批处理文件退出时,我都会在这里结束,并在进程返回非零代码时出现错误代码

So here is the answer to that:

所以这里是答案:

if %ERRORLEVEL% NEQ 0 exit %ERRORLEVEL%