The command: "LABEL" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "LABEL"
The
`LABEL` command is used in batch scripts to label a point in the code with a specific name. Here are some examples:
Example 1: Simple label:
@ECHO OFF
GOTO start
:Begin
ECHO The script has begun.
GOTO end
:End
ECHO The script is finished.
Description: Shown here is a simple script that uses two labels (
`:Start` and
`:End`). The script starts at
`:Start` and then jumps to
`:End`. You can also use
`GOTO :End`.
Example 2: Use with IF statement:
@ECHO OFF
SET option=YES
IF "%Option%"=="YES" (
GOTO YesOption
) ELSE (
GOTO No option
)
:YesOption
ECHO The option is activated.
GOTO end
:No option
ECHO The option is disabled.
GOTO end
:End
ECHO The script is finished.
Description: Here a label is combined with an IF statement. Depending on the variable
`Option` it jumps to either
`:YesOption` or
`:NoOption`.
Example 3: Use with FOR loop:
@ECHO OFF
FOR %%G IN (1 2 3) DO (
ECHO loop pass no. %%G
GOTO loop end
)
:End of loop
ECHO The FOR loop has ended.
Description: Here a label is used within a FOR loop. After the first loop run, it jumps to
`:LoopEnde` and ends the loop early.
Example 4: Use with :EOF (end of script):
@ECHO OFF
GOTO main program
:Secondary program
ECHO The secondary program is executed.
GOTO :EOF
:Main program
ECHO The main program starts.
GOTO subprogram
Description: Here a label called
`:EOF` (End of File) is used to end the script. When
`GOTO :EOF` is reached, the script exits.
Example 5: Use with CALL for subprograms:
@ECHO OFF
CALL :Subprogram
ECHO The main program continues.
GOTO end
:Subprogram
ECHO The subprogram is executed.
GOTO :EOF
:End
ECHO The script is finished.
Description: Here
`CALL` is used to call a subprogram with a label (
`:subprogram`). After the call, the main program returns and continues its execution.
Labels are useful for controlling the flow of batch scripts and organizing parts of the code. Note that labels start with a colon (
`:`) followed by a name (without spaces). The
`GOTO` statement is used to jump to a specific label.
"LABEL" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
Creates, changes, or deletes the volume label of a disk.
LABEL [drive:][label]
LABEL [/MP] [volume] [label]
drive: Specifies the drive letter of a drive.
label Specifies the label of the volume.
/MP Specifies that the volume should be treated as a
mount point or volume name.
volume Specifies the drive letter (followed by a colon),
mount point, or volume name. If volume name is specified,
the /MP flag is unnecessary.
Important information, tips for the "LABEL" command
There are a few important points to note when using `LABEL` in batch scripts:
1. Colon: A `LABEL` always begins with a colon (`:`) followed by a name. For example: `:MyLabel`. Note that there can be no spaces before the colon.
2. Case sensitivity: In batch scripts, case sensitivity is usually not relevant. The label `:MyLabel` is equivalent to `:mylabel`. However, it is advisable to maintain consistent spelling to avoid confusion.
3. Uniqueity: Labels must be unique in the script. Two labels with the same name could result in unexpected behavior. It is recommended to choose clear and descriptive names for labels.
4. Use with `GOTO`: Labels are often used with the `GOTO` statement to control script flow. Note that `GOTO` jumps to a label in the same batch script.
5. Labels with `CALL`: You can use `CALL` to jump to a label, especially if it is a subprogram. For example: `CALL :subprogram`.
6. Use with `:EOF`: `:EOF` is used as a special label at the end of a script to terminate the script. When `GOTO :EOF` is reached, the script exits execution.
7. Context Switch: When you jump to a label, a context switch occurs. This means that local variables are available within the label, while variables outside the label are not visible unless they are defined globally.
8. Labels and Loops: Labels can be used in conjunction with loops (`FOR` or `WHILE` loops) to control the flow of the script.
9. Readability: Use labels to divide the code into logical sections and improve the readability of the script. This makes the code easier to understand and maintain.
10. Structured Programming: The use of labels makes it possible to design batch scripts according to the principles of structured programming. This makes maintenance and troubleshooting easier.
It is important to consider the above points to ensure that labels are used correctly and effectively in batch scripts. A cleanly structured script makes error diagnosis and maintenance easier.