How can one batch file get another exit code?

I have two batch files, task.bat and runtask.bat . runtask.bat calls task.bat , and I would like runtask.bat to get the exit code of task.bat into a variable. How can I do that?

task.bat:

 @echo off set errorlevel=1 

runtask.bat

 ... CMD /C task.bat set taskexitcode=???? 
+6
source share
2 answers

Just replace CMD /C with call .

task.bat:

 @echo off set errorlevel=15 

runtask.bat

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

Output

 15 
+8
source

The accepted answer is correct, but if you use call to call another batch of script, and this second batch of script uses SetLocal , for this you may need parsing. If you use this, add the following code before exit b :

 ENDLOCAL&set myvariable=%myvariable% 

Now the value of myvariable becomes available to the calling context, and you can see its value in another script.

Literature:
fooobar.com/questions/255473 / ...
http://www.borngeek.com/2008/05/22/exiting-batch-file-contexts/

0
source

All Articles