Exit from nested batch file Exit from nested batch file windows windows

Exit from nested batch file


You can use a syntax error, this stop immediately the batch without closing the command window.

The :HALT functions calls the :__halt function only to supress the error message.

c.bat

@echo offecho this is batch 'c'echo An error occurscall :HALTexit /b:HALTcall :__halt 2> nulexit /b:__halt()


You might try this (c.bat):

@echo. this is batch 'c'@pauseexit


You can use errorlevels with exit codes, as described here: http://www.robvanderwoude.com/errorlevel.php

In particular, if you want to do a manual error, then c.bat or b.bat should explicitly have an exit code specified with

EXIT /b 1

(or a number of your choosing), but if you just want windows automatic errors to count, then right after running b.bat or c.bat, you can just write

IF ERRORLEVEL 1 EXIT /b %ERRORLEVEL%

Which will propagate the same error up to the next program, so they can exit immediately if you so wish. Has the advantage that you can stop propagating upwards whenever you want.

(edit: to be clear, the second line of code mentioned here is necessary in all but the bottom-level program whether you're using manual or automatic errors)