Even our favorite batch files will, at some time, need to quit.
Sometimes they need to quit based on a condition that has been or has not been met.
And every batch file needs to quit, and will do so, when it reaches the last line of code.
There are several ways to end batch file execution, like reaching the end of the batch file, starting execution of another batch file, or using the EXIT
or GOTO:EOF
commands.
Each operating system may react in its own way to any of these events.
And often there is a difference between ending batch file execution and closing the batch file's console (window).
After the batch file has executed its last line of code it will stop running, unless the last line contained a GOTO
command forcing it to re-execute some of its previous code.
When the batch file stops running it will return control to the command processor.
If that command processor was started just for the purpose of executing the batch file, the command processor itself will stop running after completing batch file execution.
This may be the case when a batch file is started by double-clicking a shortcut in Windows or OS/2.
Though the batch file may be terminated, the console (window) the batch file has been running in may be left open, depending on the operating system, the command processor, and how batch file execution was started (from a command prompt or through a shortcut).
Operating System | Command Processor | Command Prompt | Shortcut | CALLed subroutine | Remarks |
---|---|---|---|---|---|
MS-DOS 3...6.22 | COMMAND.COM | Leave command prompt open | N/A | N/A | |
MS-DOS 7.* (Windows 9x) | COMMAND.COM | Leave command prompt open | Leave console window open | N/A | Make sure no text is displayed in the console window to make it close automatically at the end of the batch file |
OS/2 | CMD.EXE | Leave command prompt open | Leave console window open | N/A | |
OS/2 | COMMAND.COM | Leave command prompt open | Leave console window open | N/A | |
Windows NT 4 | CMD.EXE | Leave command prompt open | Close console window | Return to CALLing command line | |
Windows NT 4 | COMMAND.COM | Leave command prompt open | Close console window | N/A | |
Windows 2000/XP/2003 | CMD.EXE | Leave command prompt open | Close console window | Return to CALLing command line | |
Windows 2000/XP/2003 | COMMAND.COM | Leave command prompt open | Close console window | N/A |
Using EXIT
will stop execution of a batch file and return control to the command processor (or, with NT's /B
switch, to the calling batch file) immediately.
Operating System | Command Processor | Command Prompt | Shortcut | CALLed subroutine | Remarks |
---|---|---|---|---|---|
MS-DOS 3...6.22 | COMMAND.COM | Leave command prompt open | N/A | N/A | |
MS-DOS 7.* (Windows 9x) | COMMAND.COM | Close command prompt | Leave console window open | N/A | Make sure no text is displayed in the console window to make it close automatically at the end of the batch file |
OS/2 | CMD.EXE | Close command prompt | Close console window | Close command prompt or window | |
OS/2 | COMMAND.COM | Close command prompt | Close console window | N/A | |
Windows NT 4 | CMD.EXE | Close command prompt | Close console window | Close command prompt or window | |
Windows NT 4 | COMMAND.COM | Close command prompt | Close console window | N/A | |
Windows 2000/XP/2003 | CMD.EXE | Close command prompt | Close console window | Close command prompt or window | |
Windows 2000/XP/2003 | COMMAND.COM | Close command prompt | Close console window | N/A |
Using EXIT /B
will stop execution of a batch file or subroutine and return control to the command processor or to the calling batch file or code immediately.
EXIT /B
is available in Windows 2000 and later versions' CMD.EXE only.
If followed by an integer number the code will return an exit code or ERRORLEVEL equal to that number.
Operating System | Command Processor | Command Prompt | Shortcut | CALLed subroutine | Remarks |
---|---|---|---|---|---|
MS-DOS 3...6.22 | COMMAND.COM | N/A | N/A | N/A | |
MS-DOS 7.* (Windows 9x) | COMMAND.COM | N/A | N/A | N/A | |
OS/2 | CMD.EXE | N/A | N/A | N/A | |
OS/2 | COMMAND.COM | N/A | N/A | N/A | |
Windows NT 4 | CMD.EXE | N/A | N/A | N/A | |
Windows NT 4 | COMMAND.COM | N/A | N/A | N/A | |
Windows 2000/XP/2003 | CMD.EXE | Leave command prompt open | Close console window | Leave subroutine and return to calling code | |
Windows 2000/XP/2003 | COMMAND.COM | N/A | N/A | N/A |
Here is a more detailed explanation by Timothy Dollimore:
The DOS online help (
HELP EXIT
) doesn't make it clear that the/B
parameter exits the current instance of script which is not necessarily the same as exiting the current script.
I.e. if the script is in a CALLed piece of code, theEXIT /B
exits theCALL
, not the script.To explain, one can use
EXIT /B 0
in a similar fashion toGOTO:EOF
to exit (or more accurately, return) from a called section of code inside a script.
In short, GOTO:EOF
should have the same result as reaching the end of the batch file.
It marks the end of a subroutine, and returns to the CALLing code.
GOTO:EOF
is available in Windows NT 4 and
later versions' CMD.EXE only.
Operating System | Command Processor | Command Prompt | Shortcut | CALLed subroutine | Remarks |
---|---|---|---|---|---|
MS-DOS 3...6.22 | COMMAND.COM | N/A | N/A | N/A | |
MS-DOS 7.* (Windows 9x) | COMMAND.COM | N/A | N/A | N/A | |
OS/2 | CMD.EXE | N/A | N/A | N/A | |
OS/2 | COMMAND.COM | N/A | N/A | N/A | |
Windows NT 4 | CMD.EXE | Leave command prompt open | Close console window | Leave subroutine and return to calling code | |
Windows NT 4 | COMMAND.COM | N/A | N/A | N/A | |
Windows 2000/XP/2003 | CMD.EXE | Leave command prompt open | Close console window | Leave subroutine and return to calling code | |
Windows 2000/XP/2003 | COMMAND.COM | N/A | N/A | N/A |
page last modified: 2016-09-19; loaded in 0.0012 seconds