Choice (command)
In computing,
choice is a command that allows for batch files to prompt the user to select one item from a set of single-character choices. It is available in a number of operating system command-line shells.History
The command was first introduced as an external command with MS-DOS 6.0. It is included in Novell DOS 7 and IBM PC DOS 7.0, and is also available from the command-line shell of some versions of Microsoft Windows, but not under Windows 2000 and Windows XP. It was first made available for Windows in the Windows XP Resource Kit.It has been reintroduced in Windows Server 2003 and is present in later versions.
Starting with Windows 2000, the
SET command has similar functionality using the /P command-line argument. However this command requires an additional key stroke, which is not required by choice.The
choice command has been ported to OS/2, Rexx and Perl. It is also available in Datalight ROM-DOS, FreeDOS, PTS-DOS, and ReactOS. The FreeDOS version was developed by Jim Hall and is licensed under the GPL v2.Usage
The command returns the selected choice as an exit code which is set to the index of the key that the user selects from the list of choices. The first choice in the list returns a value of 1, the second a value of 2, and so forth.If a key is pressed that is not a valid choice, the command will sound a warning beep. If an error condition is detected, an exit code value of 255 will be returned. An exit code value of 0 will be returned if the user presses + or +.
Choice displays the default choices Y and N if used without parameters.
Syntax
DOS
CHOICE choices] c,nn] Arguments:
/CchoicesSpecifies allowable keys. The default is "YN"./Tc,nnThis defaults choice to "c" after "nn" seconds.textSpecifies the prompt string to display..
/NSpecifies not to display the choices and "?" at end of prompt string./SSpecifies that choice keys should be treated as case sensitive.Microsoft Windows, ReactOS
CHOICE Arguments:
/CchoicesSpecifies allowable keys. The default is "YN"./TnnThis defaults choice to /D after "nn" seconds. Must be specified with default /D./DcThis defaults choice to 'c'./M textSpecifies the prompt string to display.
/NSpecifies not to display the choices and "?" at end of prompt string./CSSpecifies that choice keys should be treated as case sensitive.Example
The user is directed depending upon his input by evaluating the exit code using the
IF ERRORLEVEL command. The selected choice is then printed to the screen using the ECHO command.@ECHO OFF
@CHOICE /C:XYZ
IF ERRORLEVEL 3 GOTO Zpressed
IF ERRORLEVEL 2 GOTO Ypressed
IF ERRORLEVEL 1 GOTO Xpressed
GOTO end
ECHO You have pressed "X"!
GOTO end
ECHO You have pressed "Y"!
GOTO end
ECHO You have pressed "Z"!
@PAUSE