Can a batch file capture the exit codes of the commands it is invoking?

為{幸葍}努か 提交于 2019-12-03 09:45:28
@echo off
rem ...
set errorlevel=
MyApp1.exe
exit /b %errorlevel%

would be the explicit variant.

You could try using errorlevels. Some more info here.

Nate Cook

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/

%ERRORLEVEL% stores the return value of last executed command

call program.exe
echo program.exe returns "%ERRORLEVEL%"
IF %ERRORLEVEL% NEQ 0 (
  echo FAILED
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!