Downloads
47098.zip

What's the best way to terminate script flow? You can leave off an ending command statement and simply let the script terminate when it reaches the end of the file. Or, you can use a Goto :EOF (End Of File) command, Exit command, or Exit /B command. Which option you choose depends on whether flow is vertical and whether you want the command shell window to close when the script completes. Let's take a look at these termination command options.

Leaving Off an Ending Command
If the flow in your script is vertical and contains no Goto or Call commands that change the default flow, the script will automatically end after the last command at the bottom of the script is executed. If you're running a normal-flow script in an open command shell window, the script will leave the window open after it completes. If the script was launched at the Start>Run line, by a double-click, or by similar means, the command shell window will close after the script completes. In more advanced scripts that use For, Call, or Goto commands, script flow might not be strictly vertical and another termination method will work better.

Using the Goto :EOF Pointer
You can use the Goto :EOF label pointer to end script flow at the end of a script or at the end of a section of code. When a script uses a For statement with Call, Goto :EOF is useful to authoritatively terminate the script following the For command. Occasionally, I'm asked if the :EOF label needs to be added to the script. The answer is no; :EOF is a built-in label that doesn't need to be stated specifically. If the first Goto :EOF command in Listing A were commented out, script flow would continue a second time through the :Next labeled routine.

Using Exit or Exit /B
Exit is used to end the script and to close the command shell window that launched the script. Consequently, Exit is not the correct choice for ending a subroutine. You'll also need to make some adjustments if you plan to observe screen output following a script run. Many scripts use a Pause command just before the Exit command to keep the window open until another keystroke closes it. In the case of an already open command shell window, the Exit /B command terminates the script without closing the window (similar to Goto :EOF). Exit and Exit /B command behavior is the same for scripts that are launched by double-clicking or from the Start>Run line.