GOTO – Windows CMD Command

Direct a batch program to jump to a labelled line

Each GOTO… command must be terminated by a newline.

Although undocumented, GOTO :MySubroutine generally has the same effect as GOTO MySubroutine
or GOTO:MySubroutine (a colon in place of the space)

EOF

The eof label is a special case – using GOTO:eof will always transfer execution to the end of the current batch file or the end of the current subroutine.
This can be written as GOTO:eof or GOTO :eof the space is optional.

GOTO EOF and GOTO :EOF are not the same.
if you create a label called eof, the command GOTO:eof will still exit the file/routine and not jump to the label.

The command goto eof (without a colon) will jump to a label called eof, but to avoid confusion it is better to use a different name goto nextsub

When exiting a subroutine, an alternative command is EXIT /b
EXIT /b has the option to set a specific errorlevel, 0 for success, 1 or greater for an error.
EXIT /b without an ExitCode acts the same as goto:eof and will not alter the %errorlevel%

Errorlevels

If the jump is successfully made %ERRORLEVEL% = unchanged, typically this will be 0 but if a previous command set an errorlevel, that will be preserved (this is a bug).

If the subroutine Label does not exist %ERRORLEVEL% = 1

An alternative is to replace the GOTO with a CALL to a subroutine. The subroutine can contain GOTO statements as they will be safely outside the parenthesis.

GOTO breaks the & and && redirection operators.

GOTO is an internal command. If Command Extensions are disabled GOTO will no longer recognize the: EOF label

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *