How can one batch file get the exit code of another?

戏子无情 提交于 2019-12-07 00:28:27

问题


I have two batch files, task.bat and runtask.bat. The runtask.batcalls task.batand I would like runtask.bat to get the exit code of task.bat into a variable. How could this be done?

task.bat:

@echo off
set errorlevel=1

runtask.bat

...
CMD /C task.bat
set taskexitcode=????

回答1:


Just swap CMD /C for call.

task.bat:

@echo off
set errorlevel=15

runtask.bat

call task.bat
set taskexitcode=%errorlevel%
echo %taskexitcode%

Output

15



回答2:


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/



来源:https://stackoverflow.com/questions/14277566/how-can-one-batch-file-get-the-exit-code-of-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!