GOTO command

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.

Synopsis

 GOTO [:]label [file] [/Q

}

Jump to a label.

If the GOTO command is called within a command bloc (such as within a FOR loop or within a IF), then block execution is aborted and execution restart at given label.

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 OFF

:menu
ECHO MENU
ECHO 1. Choice 1
ECHO 2. Choice 2
ECHO 3. Choice 3
ECHO.
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
ECHO.
GOTO :menu

:case_1
:: Choosed 1
ECHO You choosed 1
GOTO :switch_end

:case_2
:: Choosed 2
ECHO You choosed 2
GOTO :switch_end

:case_3
:: Choosed 3
ECHO You choosed 3

:switch_end
::End of the batch

Notes

For compatibility purpose, the GOTO supports syntax that consist in not separating label from the command name.

Compatibility

This command is compatible with cmd.exe, excepted the file parameter which is only supported by Dos9, since version 0.7

See also

IF command, FOR loop, Conditional running Command list