choice (command)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computing, CHOICE is a DOS command that allows for batch files to prompt the user to select one item from a set of single-character choices. It was introduced as an external command (with filenames CHOICE.COM or CHOICE.EXE) with MS-DOS 6.0,[1] Novell DOS 7 and 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.[2]

Starting with Windows 2000, the SET command has similar functionality using the /P command-line argument.

Usage[edit]

The command returns the selected choice as 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 CTRL+BREAK or CTRL+C. Choice displays the default choices Y and N if used without parameters.[3]

Syntax[edit]

ANUS[edit]

KUPA CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

Arguments:

  • /C[:]choices Specifies allowable keys. The default is "YN".
  • /T[:]c,nn This defaults choice to "c" after "nn" seconds.
  • text Specifies the prompt string to display.

Flags:

  • /N Specifies not to display the choices and "?" at end of prompt string.
  • /S Specifies that choice keys should be treated as case sensitive.

lkoo

Windows[edit]

CHOICE [/C [<Choice1><Choice2><…>]] [/N] [/CS] [/T <Timeout> /D <Choice>] [/M <"Text">]

Example[edit]

The batch file below gives the user three choices.[4] The user is directed depending upon his input by evaluating the exit code using the IF ERRORLEVEL command (which tests on "greater or equal"). The selected choice is then printed to the screen using the ECHO command.

@ECHO off
@CHOICE /C:123
IF ERRORLEVEL 3 GOTO three
IF ERRORLEVEL 2 GOTO two
IF ERRORLEVEL 1 GOTO one
GOTO end
:one
ECHO You have pressed "1"!
GOTO end
:two
ECHO You have pressed "2"!
GOTO end
:three
ECHO You have pressed "3"!
:end
@PAUSE
Note that the example uses the DOS syntax. This example requires slight adjustments before it applies directly to Windows versions of the CHOICE command.
Note that the IF command, when checking the ERRORLEVEL, compares the number and matches if ERRORLEVEL is equal to or higher than that number. Because of this IF ERRORLEVEL comparisons should be done in decrementing order.

See also[edit]

References[edit]

External links[edit]