3

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

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.