Simulating SELECT in batch programs

[Autolink] Menu

 
@ECHO OFF
REM
REM *** sample code to show how to simulate SELECT in OS/2 Batch files
REM
REM This batch file simulates the following REXX code:
REM
REM   parse arg val
REM   if val = '' then
REM     val = varA
REM
REM   SELECT
REM
REM     WHEN val=varA THEN
REM       say 'This is 'val
REM
REM     WHEN val=varB THEN
REM       say 'This is 'val
REM
REM     WHEN val=varC THEN
REM       say 'This is 'val
REM
REM     WHEN val=varD THEN
REM       say 'This is 'val
REM
REM     OTHERWISE
REM     DO
REM       say 'Invalid parameter 'val
REM       EXIT 255
REM     END /* OTHERWISE */
REM
REM   END /* select */
REM
REM   EXIT 0
REM
REM     Note that this method is useful for processing parameter
REM     because it's not case sensitive.
REM     (see Using the SELECT simulation for parameter checking )
REM
REM History:
REM
REM  01.02.1997 /bs
REM   - added code to simulate OTHERWISE also
REM     (based on code from Ralf Ulrich (see EMail Addresses))
REM
REM

REM ------------------------------------------------------------------
REM *** Use "varA" if the parameter is omitted
REM
SET var=%1
IF '%var%' == '' SET var=varA

REM ------------------------------------------------------------------
REM *** Now goto to the appropriate label
REM     (or print an error message and exit if the label does not exist)
REM
REM

GOTO x%var% 2>NUL || (ECHO.&ECHO.Invalid parameter "%var%"!&EXIT 255)
REM                  --------------------------------------------------------
REM                  ^
REM                  This is the code for OTHERWISE
REM                  Note that you CANNOT use a GOTO command here! If the
REM                  first GOTO fails, the program is always aborted!
REM

REM *** The following are the labels for the known values
REM
REM     In this example you can use the parameter varA, varB, varC,
REM     and varD.
REM

REM ------------------------------------------------------------------
:xVarA
ECHO. This is "%var%"
GOTO HouseKeeping

REM ------------------------------------------------------------------
:xVarB
ECHO. This is "%var%"
GOTO HouseKeeping

REM ------------------------------------------------------------------
:xVarC
ECHO. This is "%var%"
GOTO HouseKeeping

REM ------------------------------------------------------------------
:xVarD
ECHO. This is "%var%"
GOTO HouseKeeping

REM ------------------------------------------------------------------
REM *** house keeping
:HouseKeeping
SET var=

REM *** and end the program
GOTO End

REM ------------------------------------------------------------------
REM *** label marking the program end
:END
EXIT 0


[Back: Using sub routines in batch programs]
[Next: Using the SELECT simulation for parameter checking]