goto - Jump to a Label

Maintained on

When writing batch files, you often want to skip some processing when a condition is met, or jump to error handling when something fails.

The goto command makes this kind of flow control possible.

This article explains the goto command, from the basics to subroutines, with sample code.

Basic syntax of goto

The goto command jumps to the position of a specified label.

GOTO label_name

A label is defined as a line that starts with a colon :.

:label_name

Basic example

@echo off
setlocal

echo Running step 1.
GOTO SKIP
echo This line is skipped.
:SKIP
echo Running step 2.

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>goto_basic.cmd
Running step 1.
Running step 2.
C:\users\user>

GOTO SKIP jumps to the :SKIP label, so the echo in between is never executed.

About label names

Label names are not case-sensitive. :SKIP, :skip and :Skip are treated as the same label.

Combining goto with conditions

The goto command is often combined with the if command to change the flow depending on a condition.

@echo off
setlocal

copy "important.txt" "backup\important.txt" >nul 2>&1
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

echo Backup completed.
GOTO END

:ERROR
echo Error: the backup failed.

:END
endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>error_handling.cmd
Error: the backup failed.
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

If the copy fails, execution jumps to :ERROR; if it succeeds, it jumps to :END.

Ending a batch file with GOTO :EOF

GOTO :EOF jumps to the end of the batch file and ends its execution.

@echo off
setlocal

IF NOT EXIST "config.ini" (
    echo Error: config.ini was not found.
    GOTO :EOF
)

echo Loaded the configuration file.
rem Further processing...

endlocal
チェック

:EOF is a special label that you do not need to define. It is available when command extensions are enabled (the Windows default).

Implementing a menu

With goto you can build a menu that runs different processing depending on the user’s choice.

@echo off
setlocal

:MENU
echo.
echo ========== Menu ==========
echo 1. List files
echo 2. Show disk usage
echo 3. Exit
echo ==========================
set "CHOICE="
set /p CHOICE=Enter a number:

IF "%CHOICE%"=="1" GOTO LIST
IF "%CHOICE%"=="2" GOTO DISK
IF "%CHOICE%"=="3" GOTO END
echo Invalid input.
GOTO MENU

:LIST
dir /b
GOTO MENU

:DISK
powershell -NoProfile -Command "Get-PSDrive -PSProvider FileSystem"
GOTO MENU

:END
echo Exiting the program.
endlocal

The menu is displayed repeatedly until the user enters “3”.

If the user just presses Enter, set /p leaves the variable unchanged and the previous value remains. That is why the variable is cleared with set "CHOICE=" before the prompt.

About wmic

wmic used to be a common way to get disk information, but it is deprecated and is no longer installed by default in newer versions of Windows 11. This example uses PowerShell instead.

Using it as a subroutine (CALL :label)

goto does not come back after jumping, but CALL :label runs a label as a subroutine (similar to a function) and returns to the original position afterwards.

@echo off
setlocal

echo Starting the main process.

CALL :GREET "Taro"
CALL :GREET "Hanako"

echo Finished the main process.
GOTO :EOF

:GREET
echo Hello, %~1!
EXIT /B
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>subroutine.cmd
Starting the main process.
Hello, Taro!
Hello, Hanako!
Finished the main process.
C:\users\user>
Ending a subroutine

A subroutine must end with EXIT /B or GOTO :EOF. Otherwise, execution continues into the following subroutines and statements.

Implementing a loop with goto

You can write a counter-based loop with only a label and goto, without the for command.

@echo off
setlocal

set /a COUNT=1

:LOOP
IF %COUNT% GTR 3 GOTO :EOF
echo Iteration %COUNT%.
set /a COUNT+=1
GOTO LOOP
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>loop.cmd
Iteration 1.
Iteration 2.
Iteration 3.
C:\users\user>

Common errors and fixes

”The system cannot find the batch label specified”

This message appears when the label named in GOTO does not exist in the batch file. Check the following:

  • The label name is spelled the same in both places (do not write the colon in GOTO; only the definition has :)
  • The label line starts with a colon
  • The label you jump to is defined outside, not inside, a block enclosed in parentheses ( )

Cautions when using goto

Avoid overuse

Heavy use of goto makes the flow hard to follow and leads to so-called “spaghetti code”. Consider replacing it with if/else or subroutines using CALL :label where possible.

Be careful inside loops

Using goto inside a for loop or a block enclosed in parentheses ( ) aborts that loop or block. If you need branching inside a loop, use an if statement.

Summary

This article explained how to use the goto command.

PurposeSyntax
Jump to a labelGOTO label
End the batch fileGOTO :EOF
Call a subroutineCALL :label
Return from subroutineEXIT /B

Practice question

Practice Problem

Select the incorrect statement about the GOTO command.

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #GOTO #Label #Commands