简体   繁体   中英

Exit a batch script that called another batch script

I have a batch script that calls a sequence of batch files.

There is an error case where I need to exit the batch file that was called, as well as the parent batch file. Is it possible to accomplish this inside the child batch file?

test.bat

rem Test1.bat will exit with error code.
call test1.bat
rem Want script to stop if test1.bat errors.
call test2.bat

test1.bat

rem Can I get test.bat to terminate from inside test1.bat?
exit /b 1

You can, by using errorlevel. If the called batches systematically use exit 0 to notify keep on and exit 1 to ask caller to stop, you can modify the caller that way:

rem Test1.bat will exit with error code.
call test1.bat

rem Want script to stop if test1.bat errors.
if errorlevel 1 goto fatal
call test2.bat
exit 0
:fatal
echo Fatal error
exit 1

You can exit all child and parent batch processes from the child by causing a fatal syntax error in the child:

test.bat

@echo off
echo before
call test1.bat
echo after

test1.bat

@echo off
echo in test 1
call :kill 2>nul

:kill - Kills all batch processing with a fatal syntax error
() rem fatal syntax error kills batch and all parents

Calling test.bat prints "before" and "in test 1", but not "after".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM