The Final Choice

Let’s wrap up our look at CHOICE.EXE and how to use it in batch files. I’ve shown you some basic examples but CHOICE has a few “advanced” features you might want to incorporate into your batch file.

As I’ve explained before, menu choices by default are case-insensitive. However, you can enforce a case-sensitive choice with the /CS switch:

@echo off
echo a = Lower Case choice
echo A = Upper case choice
Choice /c “aA” /CS /M “Make a choice”

if errorlevel 2 GOTO :Upper
if errorlevel 1 GOTO :Lower
GOTO :EOF

:Upper
echo Upper Case choice
GOTO :EOF

:Lower
echo Lower Case choice
GOTO :EOF

:EOF

The user’s choices are “a” or “A”. Again, the script jumps to the appropriate section depending on what was selected.

There may be times in your batch file where you want to provide a menu choice, but don’t want to wait indefinitely. For those situations you will use a combination of /T to specify a timeout value in seconds between 0 and 9999, and /D to indicate the default value. The /D value must be one of the options listed with /C.

@echo off
Choice /T 5 /D Y /M “Do you want to continue?”

if errorlevel 2 GOTO :NO
if errorlevel 1 GOTO :YES
GOTO :EOF

:NO
echo Not continuing
GOTO :EOF

:YES
echo Continuing
GOTO :EOF

:EOF

This prompt will wait for 5 seconds for the user to make a choice. If they don’t respond by then, the script will force the default choice of Y which will evaulated just as if the user had typed Y at the choice prompt.
By now you should have enough information and samples on how to incorporate CHOICE into your batch files. If you ever forget the options, simpy run CHOICE /? at a command prompt for syntax help.

 

Technorati tags: , , ,