choice
Windows CMD · Terminal · reference only
What it does
Shows a question with several single-key options and stores the chosen one in ERRORLEVEL, for use in .bat scripts.
Syntax
choice [/c opciones] [/m mensaje] [/t segundos /d opción] [/n] [/cs]
Options and parameters
/c opcionesLetters allowed as an answer; YN by default, depending on the language.
/m mensajeMessage shown before the question.
/nHides the list of options in brackets.
/csMakes it case-sensitive.
/t segundosSeconds to wait before choosing the default option.
/d opciónDefault option when time runs out; used with /t.
Usage examples
choice /c SN /m "¿Continuar?"Asks and waits for S or N.
choice /c SN /t 5 /d NPicks N automatically after 5 seconds if nobody answers.
if errorlevel 2 echo Has elegido NoDetects that the second option was pressed.
choice /c ABC /n /m "Elige A, B o C: "Asks for a letter without showing the options.
Notes and common mistakes
- ERRORLEVEL equals the position of the chosen option: 1 for the first, 2 for the second...
- With if errorlevel you should test from highest to lowest, because the test means greater or equal.