Can a batch file capture the exit codes of the com

2019-03-24 00:23发布

Basically, let's say that I have a batch file that calls myapp1.exe and myapp1.exe exits with Exit Code 1. Can the batch file capture this information and either force the batch file to exit with that same exit code or perform some other logic?

4条回答
做个烂人
2楼-- · 2019-03-24 01:05

You could try using errorlevels. Some more info here.

查看更多
家丑人穷心不美
3楼-- · 2019-03-24 01:08

%ERRORLEVEL% stores the return value of last executed command

call program.exe
echo program.exe returns "%ERRORLEVEL%"
IF %ERRORLEVEL% NEQ 0 (
  echo FAILED
)
查看更多
别忘想泡老子
4楼-- · 2019-03-24 01:14
@echo off
rem ...
set errorlevel=
MyApp1.exe
exit /b %errorlevel%

would be the explicit variant.

查看更多
我命由我不由天
5楼-- · 2019-03-24 01:18

The accepted answer is correct, but if you are using call to call another batch script, and that second batch script is using SetLocal, you may need to use a parsing trick to accomplish this. If you are running into this, add the following code before your exit b:

ENDLOCAL&set myvariable=%myvariable%

Now the value of myvariable is made available to the calling context and you can see the value in the other script.

References:
https://stackoverflow.com/a/16167938/89590
http://www.borngeek.com/2008/05/22/exiting-batch-file-contexts/

查看更多
登录 后发表回答