Purpose: | Branch to a specified line inside the current batch file |
Format: | GOTO [/I] label |
label | The batch file label to branch to. |
Usage:
GOTO can only be used in batch files.
After a GOTO command in a batch file, the next line to be executed will be the one immediately following the label. The label must begin with a colon [:] and appear on a line by itself, and cannot be included in a command group. The colon is required on the line where the label is defined, but is not required in the GOTO command itself. Case differences are ignored when matching labels.
This batch file fragment checks for the existence of the file CONFIG.SYS. If the file exists, the batch file jumps to C_EXISTS and copies all the files from the current directory to the root directory on A:. Otherwise, it prints an error message and exits.
if exist config.sys goto C_EXISTS
echo CONFIG.SYS doesn't exist - quitting.
quit
:C_EXISTS
copy * a:\
GOTO begins its search for the label on the line of the batch file immediately after the GOTO command. If the label is not found between that position and the end of the file, GOTO will restart the search at the beginning of the file. If the label is still not found, the batch file is terminated with the error message "Label not found."
To avoid errors in the processing of nested statements and loops, GOTO cancels all active IFF statements and DO / ENDDO loops unless you use /I. This means that a normal GOTO (without /I) may not branch to any label that is between an IFF and the corresponding ENDIFF or between a DO and the corresponding ENDDO.
For compatibility with CMD, the command
GOTO :EOF
will end processing of the current batch file if the label :EOF does not exist. However, this is less efficient than using the QUIT or CANCEL command to end a batch file.
Option:
You cannot branch into another IFF statement, another DO loop, or a different IFF or DO nesting level, whether you use the /I option or not. If you do, you will eventually receive an "unknown command" error (or execution of the UNKNOWN_CMD alias or plugin) on a subsequent ENDDO, ELSE, ELSEIFF, or ENDIFF statement.