[Toc][Index]

SWITCH - Select commands based on a value

 
 Purpose:    Select commands to execute based on a value. 
             
 Format:     SWITCH expression 
             CASE value1 [.OR. value2 ] ... 
                 commands 
             CASE value3 
                 commands 
             DEFAULT 
                 commands ] 
             ENDSWITCH 
             
             expression :  An environment variable, internal variable, 
             variable function, text string, or a combination of these 
             elements, that is used to select a group of commands. 
             value1, value2, etc. :  A value to test, or multiple values 
             connected with .OR. 
             commands :  One 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. 
 
 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 the expression.  If one of the values in a CASE statement 
 matches the expression, the commands following that CASE statement are 
 executed, and all subsequent CASE statements and the commands which 
 follow them are ignored.  If no matches are found, the commands following 
 the optional DEFAULT statement are executed.  If there are no matches and 
 there is no DEFAULT statement, no commands are executed by SWITCH. 
 After all of the commands following the CASE or DEFAULT statement are 
 executed, the batch file continues with the commands that follow 
 ENDSWITCH. 
 You must include a command separator or new line after the expression, 
 before each CASE or DEFAULT statement, before each command, and before 
 ENDSWITCH.  You can link values in a CASE statement with .OR. (but not 
 with .AND. or .XOR.). 
 For example, the following batch file fragment displays one message if 
 the user presses A, another if user presses B or C, and a third 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 not A, B, or C
         endswitch
 
 
 In the example above, the value of a single environment variable was used 
 for the expression.  You will probably find that this is the best method 
 to use in most situations.  However, you can use other kinds of 
 expressions if necessary.  The first example 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
 
 
 The SWITCH and ENDSWITCH commands must be on separate lines, and cannot 
 be placed within a command group, or on the same line as other commands 
 (this is the reason SWITCH cannot be used in aliases).  However, commands 
 within the SWITCH block can use command groups or the command separator 
 in the normal way. 
 SWITCH commands can be nested.  You can exit from all SWITCH / ENDSWITCH 
 processing by using GOTO to a line past the last ENDSWITCH. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs