Programming Field

‘:’ (Define label) - DOS/Command Prompt Reference

Defines a label by placing a ‘:’ (colon) at the head of a string in batch files.




Usage of ‘:’

Labels are used for conditional branching in the batch program. By specifying the label name to Goto command, you can change the next processing in the batch program to the line where the label is defined. The label itself has no effect when executed.

Note that the syntax is different from the one such in C language, BASIC, JavaScript; a colon ‘:’ must be in the head.

Since the label-defined line (including ‘:’ character) does not affect the execution, this syntax is sometimes used to write comments. (The line starting with two or more colons does not work for valid label (not jumpable), so you can use these as comment-leading characters safely.)


Sample 1 (Batch file)

foo.exe /test
if errorlevel 1 goto Error
echo No errors occurred.
goto End
echo An error has occurred.

Executes ‘foo.exe /test’, and prints the message depending on the command result (whether the exit code is 1 or larger) by using If.

Sample 2 (Batch file)

goto %config%
lh mouse.exe
goto common
lh mouse.exe
lh mscdex.exe /D:CDROM1
goto common
lh nlsfunc.exe country.sys

Executes processing according to the value of the environment variable ‘config’.

In MS-DOS, the environment variable ‘config’ has the value of the execution mode defined in Config.sys. (The execution mode is selected by the user from ‘Startup menu’.) This style is commonly used in Autoexec.bat.

Sample 3 (Batch file)

:: This is a comment.
:: This is also a comment.
@echo off
echo Hello

This is a sample using ‘:’ character as a commend-leading character. The line starting with ‘:’ character is not printed (echo-backed), and you cannot jump to the line starting with ‘::’ by using Goto, so you can use ‘::’ as a strict comment-leading character.

See also