GOTO – Windows CMD Command
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Direct a batch program to jump to a labelled line
1 2 3 4 5 6 7 8 9 10 11 |
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)
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Examples: A simple goto jump: GOTO sub_message Echo this wont display goto:eof :sub_message Echo this is a subroutine Use the %1 parameter to jump: IF %1==12 GOTO specialcase Echo the input was NOT 12 goto:eof :specialcase Echo the input was 12 goto:eof Use a variable as a label: CHOICE /C:01 /m choose [Y]yes or [N]No goto sub_%ERRORLEVEL% :sub_0 Echo You typed Y for yes goto:eof :sub_1 Echo You typed N for no goto:eof |
1 |
Use a variable as a comment |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 ... Bugs Using GOTO within parenthesis - 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 parenthesis.
GOTO breaks the & and && redirection operators.
1 2 3 4 |
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. |
GOTO is an internal command. If Command Extensions are disabled GOTO will no longer recognize the: EOF label
Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.