Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit from nested batch file

I have 4 batch files, suppose a.bat, b.bat, c.bat and d.bat. Now these batch files are called in such a manner that a.bat calls b.bat, b.bat calls c.call and so on.

If I get any error in any batch file, I want to exit from the entire program by saying an error occurred, and mention which batch file had a problem. My question is, how can I do this?

Here I used exit /b but it only gets out from the current batch file and moves back into batch file from whence it was called:

a.bat

@echo. off
echo. this is batch 'a'
call b.bat

b.bat

@echo. off
echo. this is batch 'b'
call c.bat

c.bat

@echo. off
echo. this is batch 'c'

I get an error in batch 'C' - It should then report an error and exit, but it's moving back into batch 'B' somehow. Any idea on how to exit from a nested batch file?

like image 736
user2583646 Avatar asked Jul 15 '13 13:07

user2583646


People also ask

How do you exit a batch file?

EXIT /B at the end of the batch file will stop execution of a batch file. use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.

What is @echo off in batch?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

How do I stop a batch file from another batch file?

Ctrl+C. One of the most universal methods of aborting a batch file, command, or another program while it's running is to press and hold Ctrl + C . This keyboard shortcut sends a SIGINT signal, which cancels or terminates the currently-running program and returns you to the command line.

How do you exit a file in cmd?

To close or exit the Windows command line window, also referred to as command or cmd mode or DOS mode, type exit and press Enter . The exit command can also be placed in a batch file.


1 Answers

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 off
echo this is batch 'c'
echo An error occurs
call :HALT
exit /b

:HALT
call :__halt 2> nul
exit /b

:__halt
()
like image 85
jeb Avatar answered Sep 20 '22 10:09

jeb