Purpose:Select commands to execute in a batch file based on a value

 

Format:SWITCH expression

CASE value1 [.OR. value2 [.OR. value3 ...]]

[commands]

CASE value4

[commands]

CASEALL

[commands]

[DEFAULT

commands]

ENDSWITCH

 

expressionAn environment variable, internal variable, variable function, text string, or a combination of these elements, that is used to select a group of commands.
value1, value2A value to test or multiple values connected with .OR.
commandsOne or more commands to execute if the expression matches the value. If you use multiple commands, they must be separated by command separators or placed on separate lines of a batch file.

 

See also: IF and IFF.

 

Usage:

 

SWITCH can only be used in batch files. It allows you to select a command or group of commands to execute based on the possible values of a variable or a combination of variables and text.

 

The SWITCH command is always followed by an expression created from environment variables, internal variables, variable functions, and text strings, and then by a sequence of CASE statements matching the possible values of expression, an optional DEFAULT statement, and terminated by an ENDSWITCH statement. Each CASE statement and the DEFAULT statement may be followed by one or more commands.

 

TCC evaluates expression, and sequentially compares it with the list of values in the CASE statements, starting with the first one. Comparison rules are the same ones used for the EQ relational operator; see Numerical and String Comparisons for details. If a match is found, the commands following the matched CASE statement are executed, and the batch file continues with the commands that follow ENDSWITCH. If there are any matches in subsequent CASE statements, they are ignored. The value in a CASE statement can be literals, or variables or functions (which will be expanded prior to the comparison with the SWITCH expression).

 

CASE statements can include wildcards and regular expressions.

 

The optional CASEALL statement should follow all of the CASE statements but precede DEFAULT. If any preceding CASE block was executed, CASEALL will also be executed; otherwise it is ignored.

 

If during the search for a match the DEFAULT statement is encountered, the commands, if any, following it are executed, and the batch file continues with the commands that follow ENDSWITCH. Any CASE statements after the DEFAULT statement are ignored.

 

SWITCH commands can be nested.

 

You can exit from all SWITCH / ENDSWITCH processing by using GOTO to a line past the last ENDSWITCH.

 

Restrictions

 

Each SWITCH, CASE, DEFAULT and ENDSWITCH statement must be on a separate line, and may not be followed by a command separator. (This is the reason SWITCH cannot be used in aliases.) There is no restriction on grouping and command separator use in the commands for a CASE or DEFAULT.

 

You can link a list of values in a single CASE statement with .OR., but not with .AND. or .XOR..

 

Examples:

 

The batch file fragment below displays one message if the user presses A, another if the user presses B or C, and a third one if the user presses any other key:

 

inkey Enter a keystroke: %%key

switch %key

case A

  echo It's an A

case B .or. C

  echo It's either B or C

default

  echo It's none of A, B, or C

endswitch

 

In the example above, the value of a single environment variable was used for expression. However, you can use other kinds of expressions if necessary. The first SWITCH statement below selects a command to execute based on the length of a variable, and the second bases the action on a quoted text string stored in an environment variable:

 

switch %@len[%var1]

case 0

  echo Missing var1

case 1

  echo Single character

...

endswitch

 

 

switch "%string1"

case "This is a test"

  echo Test string

case "The quick brown fox"

  echo It's the fox

...

endswitch