Prompts the user to make a choice in a batch program. Displays a specified prompt and pauses for the user to choose from among a specified set of keys. You can use this command only in batch programs.
CHOICE [/C[:]keys] [/N] [/S] [/T[:]c,nn] [text]
CHOICE [/C keys] [/N] [/CS] [/T c /D nn] [/M text]
For more information on the
ERRORLEVEL parameter,
and the IF command.
Replaced starting with
WinXP with
SET
/P.
The first key you assign returns a value of 1, the second a value of 2, the third a value of 3, and so on. If the user presses a key that is not among the keys you assigned, CHOICE sounds a warning beep (that is, it sends a BEL, or 07h, character to the console).
If CHOICE detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK (^BREAK) or CTRL+C (^C), CHOICE returns an ERRORLEVEL value of 0.
When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
Choice.com was originally supplied on the Windows 95 install CD (and DOS v6.0 before that) and the same utility has also been released in the NT resource kits, hovever there are some problems under NT - multiple concurrent invocations of CHOICE will clobber each other. Choice.com will also burn a lot of CPU's when in a wait state.
If you use this syntax in a batch file,
CHOICE /C:YNC
the user sees when CHOICE is started:
[Y,N,C]?
If you add text to the syntax,
CHOICE /C:YNC Yes, No, or Continue
the user sees when CHOICE is started:
Yes, No, or Continue [Y,N,C]?
If, you use the /N switch to leave out the prompt in a batch program,
CHOICE /N Yes, No, or Continue?
the user sees only the text you specified when CHOICE is started:
Yes, No, or Continue?
If you use this syntax in a batch program,
CHOICE /C:YNC /T:n,5
the user sees when CHOICE is started:
[Y,N,C]?
If, after 5 seconds, the user hasn't pressed a key, CHOICE chooses N and returns an ERRORLEVEL value of 2. If the user presses a key before 5 seconds, CHOICE returns the value corresponding to the user's choice.
To have the option of defragmenting drive C when you start your computer, you could add these lines to your AUTOEXEC.BAT file:
CHOICE Defrag drive /T:y,5 IF ERRORLEVEL 2 GOTO SKIPDEFR DEFRAG C: :SKIPDEFR
If you press N within 5 seconds, DEFRAG will not run and CHOICE returns an ERRORLEVEL value of 2. If you do not press N within 5 seconds, or if you choose Y, DEFRAG is run on drive C.
This batch program demonstrates using the CHOICE option to select one of three programs: MS-DOS Editor, Microsoft Anti-Virus, or Microsoft Backup.
Notice that the IF ERRORLEVEL statements are listed in decreasing order. The operating system will consider the IF statement true if the ERRORLEVEL parameter returned by CHOICE is greater than or equal to the parameter specified in the IF command.
@ECHO OFF CLS ECHO. ECHO A Microsoft Editor ECHO B Microsoft Anti-Virus ECHO C Microsoft Backup ECHO. CHOICE /C:abc Choose an option IF ERRORLEVEL 3 GOTO MSBACKUP IF ERRORLEVEL 2 GOTO MSAV IF ERRORLEVEL 1 GOTO EDIT :EDIT EDIT GOTO END :MSAV MSAV GOTO END :MSBACKUP MSBACKUP GOTO END :END
CHOICE can be used to set a specific ERRORLEVEL for example to set the ERRORLEVEL to 6:
ECHO6|CHOICE /C:123456 /N >NUL