Direct a batch program to jump to a labelled line.
Syntax GOTO label GOTO:eof Key label A predefined label in the batch program. Each label must be defined on a line by itself, beginning with a colon and ending with either a space, a colon or a CR/LF. :eof This predefined label will exit the current subroutine or script.
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).
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 sucess, 1 or greater for an error.
EXIT /b without an ExitCode acts the same as goto:eof and will not alter the %errorlevel%
The goto command has a poor reputation, with a tendency to produce spaghetti code. A good practice is to place all subroutines towards the end of the script, end each subroutine with a goto :eof and then place another goto :eof before the first subroutine. Then use the CALL command rather than goto.
This will ensure that the code flows in a predictable pattern like a more formal programming language.@Echo Off :: main routine CALL :subroutine1 CALL :subroutine2 goto:eof :: end of main routine. :subroutine1 Echo some commands here. goto :eof :subroutine2 Echo some more commands here. goto :eofIn a real-world example you might call the subroutines multiple times, but the structure remains the same.
Using GOTO within parentheses - including FOR and IF commands - will break their context:
@Echo Off if A equ A ( GOTO :EXAMPLE_LABEL :EXAMPLE_LABEL rem ) else ( echo You didn’t expected to see this,did you? )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 parentheses.
GOTO breaks the & and && redirection operators.
If GOTO a non existent label is used in conjunction with a negative conditional execution, the line containing the GOTO will be executed, but the rest of the Batch file is cancelled:
goto :non_existent_label || Echo This line will run anything except GOTO ,SHIFT ,SETLOCAL , ENDLOCAL , CALL :SUBROUTINE echo This will be never displayed.Just placing a :label within parentheses, can cause errors if the following line is not a valid command, details on SO.
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
A simple goto jump:
@Echo Off GOTO sub-message Echo this wont display goto:eof :sub-message Echo this is a subroutine goto:eof
Use the %1 parameter to jump:
@Echo Off IF %1==12 GOTO specialcase Echo the input was NOT 12 goto:eof :specialcase Echo the input was 12 goto:eof
Use a variable, in this case %ERRORLEVEL% to jump to a specific label:
@Echo Off
CHOICE /C:rg /m choose "[R]ed or [G]reen"
:: at this point the errorlevel will contain either 1 or 2
goto sub_%ERRORLEVEL%
:sub_1
Echo You typed R for red.
goto:eof
:sub_2
Echo You typed G for green.
goto:eof
An alternative to using a GOTO statement is using a variable to insert a comment, which will skip a line of code.
In this example the COPY command will only run if the parameter "Update" is supplied to the batch:
@Echo Off Setlocal SET _skip= IF /I NOT %1==Update SET _skip=:: %_skip% COPY x:\update.dat %_skip% echo Update applied ...
GOTO is an internal command. If Command Extensions are disabled GOTO will no longer recognise the :EOF label.
“GOTO... how bad can it be??...” ~ XKCD
CALL - Call one batch program from another.
EXIT - Quit the current script/routine and set an errorlevel.
IF - Conditionally perform a command.
Equivalent PowerShell: While (condition) {action} else {action}
Equivalent bash command: case - Conditionally perform a command.