GoTo

Index

Directs Windows to a labelled line in a batch program.

Syntax:

GOTO label
GOTO:label
labelAn alphanumeric string marking the point in a batch file to which execution should branch.
label must match a label (ie. the same string but prefixed with a colon) in the batch program. If no such label exists, the batch program stops and an error message is displayed.
label-If the batch file label is less than 7 characters and contains no delimiters, batch file processing resumes after the line following label.

Notes:

  1. Although there are few restrictions on the make up of a label, GOTO is the only command to use labels and imposes its own rules:
    • There must be a delimiter between GOTO and label. This can be one or more of any of the standards (space, comma, TAB, equal sign, or semicolon). A colon immediately following GOTO and/or immediately before label (with or without other delimiters between) is also acceptable¹ so that GOTO step1 and GOTO:step1 are considered equivalent.
      This can be useful when passing a GOTO command as an argument in a batchfile: GOTO:step1 would be passed as a single argument (thanks to Bob Blackledge for sharing this) whereas GOTO step1 would have to be passed as two.

    • GOTO only recognizes the first 8 characters in the first word of the label (ie. to the second occurrance of a recognized delimiter group - the first being between GOTO and the label). Subsequent characters and/or words are ignored. Thus, the labels :hi_there1 and :hi_there2 are both equivalent to :hi_there and the labels :hi Jim and :hi Mary are both equivalent to just :hi.

  2. GOTO looks for the first instance of a label to match label starting from the beginning of the batch file - NOT from where it is called. So, if :hi_there2 follows :hi_there1 in the batch file, GOTO hi_there2 would actually branch to :hi_there1 - always!

Example:

To format a disk in drive A and display an appropriate message of success or failure:

@ ECHO off
FORMAT a: /s
IF NOT errorlevel 1 GOTO success
ECHO An error occurred during formatting.
GOTO end
:success
ECHO Successfully formatted the disk in drive A.
:end

File Details:

Internal


This page last revised:
January 1, 2003.