Page 1 of 1

bat1 exec bat2(exit 1) , End together

Posted: 20 Jul 2020 21:42
by booboo
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%

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

Posted: 21 Jul 2020 04:00
by penpen
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