Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 of 4
http://www.robvanderwoude.com/choice.php
CHOICE
The CHOICE command was introduced in MS-DOS 6 and is still available in MS-DOS 7 (Windows 95/98).
In Windows NT 4, 2000 and XP, CHOICE is no longer a part of the standard distribution. It is, however, available as part of the Windows NT 4 Resouce Kit.
On the other hand, if you still have that old unused MS-DOS 6 or Windows 95/98 version lying around, you can use the CHOICE.COM from that version instead. (*)
Just copy it to a directory that is in your PATH.
Note: 16-bit DOS versions will not work in 64-bit Windows versions.
CHOICE is available again in Windows Vista and later versions.
Syntax:
3/15/2015 11:20 PM
2 of 4
http://www.robvanderwoude.com/choice.php
Description:
This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Parameter List:
/C choices
/N
/CS
/T timeout
/D default
/M text
The ERRORLEVEL is set to the offset of the index of the key that was selected from the set of choices.
The first choice listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool sounds a warning beep.
If tool detects an error condition, it returns an ERRORLEVEL value of 255.
If the user presses CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value of 0.
When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
3/15/2015 11:20 PM
3 of 4
http://www.robvanderwoude.com/choice.php
Examples:
The command:
CHOICE /M "Do yo really want to quit"
Will display the following line:
Do yo really want to quit? [YN]
If the user presses Y, CHOICE exits with return code ("errorlevel") 1 (1st character in choices), if the user presse N, CHOICE exits with return code 2 (2nd character in
choices).
CHOICE /C ABCDN /N /T 10 /D C /M "Format drive A:, B:, C:, D: or None?"
IF ERRORLEVEL 1 SET DRIVE=drive A:
IF ERRORLEVEL 2 SET DRIVE=drive B:
IF ERRORLEVEL 3 SET DRIVE=drive C:
IF ERRORLEVEL 4 SET DRIVE=drive D:
IF ERRORLEVEL 5 SET DRIVE=None
ECHO You chose to format %DRIVE%
The CHOICE command in this example will prompt the user with the following line:
Format drive A:, B:, C:, D: or None?
If the user presses C, CHOICE exits with a return code ("errorlevel") 3 (3rd character in choices).
The IF ERRORLEVEL checks for 1, 2 and 3 are true (see the errorlevel page for an explanation of errorlevels), so the variable DRIVE will be set to "drive A:" first, then to
"drive B:", and finaly to "drive C:".
So the ECHO command will display the line:
You chose to format drive C:
3/15/2015 11:20 PM
4 of 4
http://www.robvanderwoude.com/choice.php
By the way, in Windows NT 4 this won't work, since the SET command itself will set an errorlevel (usually 0)!
However, Windows NT makes it easy by storing the latest errorlevel in the environment variable ERRORLEVEL:
SET DRIVENUM=%ERRORLEVEL%
IF %DRIVENUM% EQU 1 SET DRIVE=drive
IF %DRIVENUM% EQU 2 SET DRIVE=drive
IF %DRIVENUM% EQU 3 SET DRIVE=drive
IF %DRIVENUM% EQU 4 SET DRIVE=drive
IF %DRIVENUM% GTR 4 SET DRIVE=None
A:
B:
C:
D:
At the bottom of my errorlevel page you can find an example that uses CHOICE to convert redirected output to an errorlevel.
On my wait page CHOICE's time-out option (/T) is used to insert a delay in batch files.
An ingenious way to use CHOICE is demonstrated by Laurence Soucy (http://users.telenor.dk/~dsl645578/batfiles.htm)'s version of BootDriv.bat.
CHOICE can also be used to strip or replace certain characters from text strings, as explained on my Batch HowTo page, the CD-ROM example of my Solutions found at
alt.msdos.batch page, and in the GetPorts example.
My PMCHOICE for NT, written in both KiXtart and batch, gives NT users almost the same functionality.
I haven't figured out a way to implement the time-out period in KiXtart, though pressing Enter will result in the default value, if specified.
My CHOICE.EXE for OS/2, written in Rexx and compiled by RXCLS, gives OS/2 users the same functionality.
To use the /T switch you need Quercus Systems' (http://www.quercus-sys.com/) RexxLib (http://www.quercus-sys.com/rxlbdemo.htm), though.
I also "ported" the CHOICE command to Rexx and Perl, though the latter does have some limitations.
3/15/2015 11:20 PM