Programming Field

Goto - DOS/Command Prompt Reference

Jumps execution to the line with the specified label name in the batch file.


goto <label-name>

Specifies the label name in the line to jump to. ‘:’ character is not necessary in <label-name>.

[MS-DOS, Windows 95/98/Me] For the label name, only the first 8 characters are used. Execution will jump to the first label matching with the first 8 characters.

[Windows NT series][Extensions] By specifying ‘:EOF’ (one colon and EOF) to <label-name>, Goto will jump to the end of the batch file regardless of existing labels. This means that executing ‘goto :EOF’ will finish the batch file or subroutine (called by Call) process. Note that only this syntax requires a colon; otherwise Goto will try to jump to ‘EOF’ label.

* In MS-DOS, Windows 95/98/Me, or the environment with Extensions disabled, ‘goto :EOF’ syntax cannot be used. ‘goto :EOF’ may not work unless Extensions are enabled by using Setlocal explicitly, so to make sure to finish the process instead of using ‘goto :EOF’, write the label in the last of the batch file explicitly and then jump to it, or call ‘exit /B’ for terminating.


This command is usually used with If for conditional processing. Normally batch files are processed line by line from the head, but Goto enables to skip lines or back to the previous line. When branching processes for condition, to skip ‘lines not to process’, Goto is necessary.

To define labels, write a ‘:’ (colon) on the beginning of the line, followed by the label name, as follows. (This is unlike the syntax of common programming languages.)

echo Reached to MyLabel

To jump to this label, write like this:

goto MyLabel

[Windows NT series][Extensions] Call command Extensions syntax can also be used to jump to the label, but Call can return back to the (almost) previous line by using Exit. For Goto there is no way to return back to the next line automatically, so you need to write another label to return back.


Sample 1 (Batch file)

@echo off
echo Input any text and type Ctrl+Z then Enter.
type CON > temp1.dat
find "7" temp1.dat > NUL
if errorlevel 1 goto NotFound
echo The text has a lucky number.
goto OnExit

echo The text was a regular string.

del temp1.dat

This is a little program to search for ‘7’ in the input text by using Find command exit code.

Sample 2 (Batch file)

@echo off
setlocal enableextensions
set COUNT=0

set /A COUNT="COUNT + 1"
set /A P="COUNT %% 3"
set /A Q="COUNT %% 5"
if %P%%Q%==00 echo FizzBuzz & goto LoopCheck
if %P%==0 echo Fizz & goto LoopCheck
if %Q%==0 echo Buzz & goto LoopCheck
echo %COUNT%
if %COUNT% LSS 30 goto MainLoop

[Windows NT series][Extensions] This is a sample of ‘Fizz Buzz’ displaying up to 30 by a batch file. This batch file has a finite loop consisting of Goto, If, and arithmetic syntaxes of Set, allowing to repeat a process between ‘MainLoop’ label and the next line of ‘LoopCheck’ label.

* This is just ‘a sample for Goto command’, meaning this is not the best program for ‘Fizz Buzz’. To make a finite loop using numbers, using ‘for /L’ may be suitable.
* The statements for setting values to ‘P’ and ‘Q’ in 7 to 8 lines use ‘%%’ instead of ‘%’ to calculate a remainder. Using ‘%%’ instead of ‘%’ is necessary for batch files. For more information about ‘%’ usage in batch files, see also ‘%’ page.

See also