CHOICE.exe

Accept user input to a batch file. Choice allows single key-presses to be captured from the keyboard.

    CHOICE [/c [choiceKeys]] [/N] [/CS] [/t Timeout /D Choice] [/M "Text"]

key
   /C[:]choiceKeys  One or more keys the user can press. Default is YN.

   /N               Do not display choiceKeys at the end of the prompt string.

   /CS              Make the choiceKeys Case Sensitive.

   /T Timeout       Timeout in Timeout seconds
                    If Timeout is 0 there will be no pause and the
                    default will be selected.

   /D choice        Default choice made on Timeout.

   /M "text"        Message string to describe the choices available.

ERRORLEVEL will return the numerical offset of choiceKeys, this can be used to set a specific %errorlevel%.

If you need to include a quotation mark within the message string, it needs to be escaped by doubling it: "message""withquote".

Examples

Prompt to select a CD or hard drive destination and goto a specific subroutine:

CHOICE /C ch /M "Select [C] CD or [H] Hard drive"
If %ERRORLEVEL% EQU 2 goto sub_hard_drive
If %ERRORLEVEL% EQU 1 goto sub_cd

Select Red or Green and goto a specific subroutine:

CHOICE /C:rg /m choose "[R]ed or [G]reen"
:: at this point the errorlevel will contain either 1 or 2
goto sub_%ERRORLEVEL%

:sub_1
Echo You typed R for red.
goto:eof

:sub_2
Echo You typed G for green.
goto:eof

Set the %ERRORLEVEL% to 6:

ECHO 6| CHOICE /C 123456 /N >NUL

“If you limit your choices only to what seems possible or reasonable, you disconnect yourself from what you truly want, and all that is left is compromise” ~ Robert Fritz

Related commands

IF - Conditionally perform a command.
CON - Accept console input.
SET /P - Prompt for user input (accepts a whole string instead of one keypress).
TIMEOUT - Delay processing of a batch file/command.
Equivalent PowerShell: MessageBox - Display a message box. / Read-Host - Read a line of input from the console.
Equivalent bash command (Linux): case / select - Accept keyboard input.


 
Copyright © 1999-2024 SS64.com
Some rights reserved