goto & Labels
A label is a line that starts with a colon, such as :cleanup. It marks a position in the batch file. goto cleanup jumps there and continues running from the line after the label. Before for loops and call grew their modern features, goto was how batch files did loops, branches and error handling, and it's still the simplest tool for "jump to the end" logic.
@echo off
echo step 1
goto finish
echo this line is skipped
:finish
echo step 2step 1
step 2
Label rules
- A label starts with
:at the beginning of a line (leading spaces are allowed) and ends at the first space. - Labels are case-insensitive:
goto Donefinds:done. gotosearches forward from the current line, wraps around to the top of the file, and jumps to the first match. Keep labels unique.- Jumping to a label that doesn't exist prints
The system cannot find the batch label specified - nameand ends the script. - A label is not a function. Execution simply flows into a label if it reaches it, so code after a label runs even without a
goto.
That last rule matters. Here the "error" section runs after the happy path because nothing stops the script before it:
@echo off
echo doing work
:error
echo something went wrongdoing work
something went wrong
The fix is to end the main part explicitly, with goto :eof (next section) or exit /b.
goto :eof
:eof ("end of file") is a built-in label that you never write yourself. goto :eof jumps past the last line, which ends the batch file (or the current subroutine, see call & Subroutines). Note the colon: goto :eof with the colon is required for this special label.
@echo off
set "file=config.ini"
if not exist "%file%" goto missing
echo found %file%
goto :eof
:missing
echo %file% is missing, nothing to doconfig.ini is missing, nothing to do
This "check, jump to an error section, otherwise goto :eof at the end of the main part" shape is the backbone of many batch scripts.
Loops with goto
A label above the code and a goto back to it makes a loop. Use if to decide when to stop:
@echo off
set /a n=1
:loop
echo pass %n%
set /a n+=1
if %n% leq 3 goto loop
echo done after %n% passespass 1
pass 2
pass 3
done after 4 passes
Unlike a for block, every pass re-reads the lines from the file, so %n% is always current. No delayed expansion is needed. That makes goto loops a good fit for "repeat until something is true", such as waiting for a file to appear or retrying a command:
@echo off
set /a attempt=0
:retry
set /a attempt+=1
echo attempt %attempt%: looking for ready.flag
if %attempt% equ 2 > ready.flag echo ok
if not exist ready.flag (
if %attempt% lss 5 goto retry
echo giving up
goto :eof
)
echo ready.flag appeared after %attempt% attemptsattempt 1: looking for ready.flag
attempt 2: looking for ready.flag
ready.flag appeared after 2 attempts
In a real script you'd pause between attempts, for example with timeout /t 5 /nobreak >nul.
A menu
goto with a computed label name turns a choice into a jump. Here the script is fed a fixed answer, so it can run in the playground; in real use the value would come from set /p or choice (see set /p: User Input):
@echo off
set "pick=2"
goto option_%pick%
:option_1
echo You chose: backup
goto end
:option_2
echo You chose: restore
goto end
:option_3
echo You chose: quit
goto end
:end
echo menu finishedYou chose: restore
menu finished
If the user types something without a matching label, goto fails and the script stops. Check the value against the allowed list first, for example with if not "%pick%"=="1" if not "%pick%"=="2" ....
goto inside blocks breaks the block
When goto runs inside parentheses (an if block or a for body), CMD abandons the whole block, including the rest of a for loop. That's how you "break" out of a loop:
@echo off
for %%f in (one two three four) do (
echo checking %%f
if "%%f"=="two" goto found
)
echo not found
goto :eof
:found
echo stopped at twochecking one
checking two
stopped at two
It also means you can't goto from inside a for body to a label and then continue the loop. For per-item logic, use call :label instead (next lesson), which returns to the loop afterwards.
Comments with ::
You'll often see comments written as :: instead of rem. A line starting with :: is technically an invalid label that goto can never reach, so CMD skips it. It's slightly faster than rem and visually distinct, but it can cause errors inside parenthesised blocks, where it's not a valid statement. Use rem inside blocks:
@echo off
:: this is a comment line
rem this is also a comment
if 1==1 (
rem inside a block, use rem
echo block ran
)block ran
When to avoid goto
goto makes it easy to write "spaghetti code" where it's hard to follow the flow. Good habits:
| Use goto for | Prefer something else for |
|---|---|
| jumping to an error/cleanup section | counting loops: for /l |
ending the main part: goto :eof |
loops over files or lines: for, for /f |
| retry loops that test a condition | reusable logic: call :label |
a dispatch table: goto option_%x% |
deeply nested branches: split into subroutines |
Bash has no goto at all; its scripts use while loops, functions and exit instead. PowerShell has no goto either, though loops can be labelled for break.
Learn more: Bash Loops, PowerShell Loops, call & Subroutines.
Test yourself
What does goto :eof do in the main part of a batch file?
- Jumps to a label you must define as :eof
- Ends the batch file
- Restarts the script
- Prints end of file
A script has no goto before :error and reaches that label. What happens?
- The script stops
- The label is skipped together with its code
- The code after :error runs too
- CMD reports a syntax error
Why are goto loops not affected by the 'variables in blocks expand early' problem?
- goto enables delayed expansion
- Each jump makes CMD read the lines again, expanding %var% freshly
- goto only works with set /a
- They are affected