Jump to a label in a batch script. This command is among the first commands learned while learning batch script, and it is usefull since it allow conditional execution.
It might be preferable to use multiline if, especially in the case of a quite short piece of instructions.
GOTO [:]label [file] [/Q
Jump to a label.
label : the label where Dos9 will jump. A label is a word, or even a couple of words, which are used to identify the different pieces of codes, and this couple of words must be preceded by a column :. It is not necessary for labels to start at column 1.
/Q : Silently ignore warning if the given label is not found on the given file and continue script execution at next line. However, it sets %ERRORLEVEL%, to 0 if jump is actually perform or -1 if it does not. This switch enables creating a kind of switch/case statement.
Constructing a switch/case statement
The GOTO command allows constructing conditional statements such as switch/case. This functionality relies on the fact that [:]label is subject to expansion, as any other batch-language element. Thus, using the /Q switch, a batch using somewhat of a switch/case statement can be achieved.
ECHO 1. Choice 1
ECHO 2. Choice 2
ECHO 3. Choice 3
SET /P choice=Make your choice:
:: start of switch/case
GOTO /Q :case_%choice%
:: if we got bad input, echo an error
:: and go back to menu
ECHO %choice% is not a valid choice
:: Choosed 1
ECHO You choosed 1
:: Choosed 2
ECHO You choosed 2
:: Choosed 3
ECHO You choosed 3
::End of the batch
For compatibility purpose, the GOTO supports syntax that consist in not separating label from the command name.
This command is compatible with cmd.exe, excepted the file parameter which is only supported by Dos9, since version 0.7