bat1 exec bat2(exit 1) , End together

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booboo
Posts: 1
Joined: 20 Jul 2020 21:04

bat1 exec bat2(exit 1) , End together

#1 Post by booboo » 20 Jul 2020 21:42

I want to get the exit code of 'bat2'
But after 'bat2' ends, 'bat1' ends with it


---bat1----

Code: Select all

call "command" > result.txt
call bat2
echo %ERRORLEVEL%		::Not executed
PAUSE				::Not executed
--bat2----

Code: Select all

@echo off

SET RESULT=

for /f "delims=" %%a in (result.txt) do call :CallBack "%%a"
GOTO End


:CallBack
echo %1
SET RESULT=%RESULT%%1
GOTO :EOF

:End
SET CODE=1
(echo %RESULT% | findstr /i /c:"Complete" >nul) && (SET CODE=0) || (SET A=1)
echo %CODE%
exit %CODE%

penpen
Expert
Posts: 1846
Joined: 23 Jun 2013 06:15
Location: Germany

Re: bat1 exec bat2(exit 1) , End together

#2 Post by penpen » 21 Jul 2020 04:00

On default (== when used without parameters) the exit-command exits the actual command interpreter (cmd.exe instance).
If you want to exit the current batch script only, then you have to use the argument "/b" instead:

Code: Select all

exit /b %CODE%
Alternatively you could start a command interpreter instance instead of calling "bat2":

Code: Select all

cmd /kbat2
The first solution wastes less resources, so i typically would prefer that.


penpen

Post Reply