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:
@echo. off
echo. this is batch 'a'
call b.bat
@echo. off
echo. this is batch 'b'
call 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?
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.
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.
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.
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.
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
()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With