What Choice Does
choice displays a prompt and waits for a single keypress from a restricted set of options, then sets errorlevel to the 1-based position of the key pressed, so choice /c YNC waiting for Y, N, or C sets errorlevel to 1, 2, or 3 respectively. This is more robust than set /p for menu selection because choice validates the keypress against the allowed set automatically, rejecting anything else without needing manual validation logic in the script. The default option set, if /c is omitted, is just Y and N, mirroring the classic yes/no confirmation prompt.
Cricket analogy: An umpire's decision review system offering exactly three buttons -- Out, Not Out, Umpire's Call -- and rejecting any other input is like choice /c ONU, restricting input to a fixed valid set automatically.
@echo off
echo Choose an action:
echo [Y] Yes [N] No [C] Cancel
choice /c YNC /n /m "Your choice: "
if errorlevel 3 goto CANCELLED
if errorlevel 2 goto NO
if errorlevel 1 goto YES
:YES
echo You chose Yes.
goto END
:NO
echo You chose No.
goto END
:CANCELLED
echo Cancelled.
:END
When checking choice results with if errorlevel, always test from the highest number down to the lowest (errorlevel 3 before errorlevel 2 before errorlevel 1), because if errorlevel N matches N and anything higher, so testing low-to-high would always match the first branch.
Timeouts, Defaults, and Case Sensitivity
The /t switch adds a timeout in seconds combined with /d to specify a default choice if the user does not respond in time, useful for unattended scripts that should proceed automatically rather than hang forever waiting for input: choice /c YN /t 10 /d Y waits 10 seconds then auto-selects Y. By default choice is case-insensitive, but /cs makes it case-sensitive, distinguishing an uppercase Y from a lowercase y as different valid keys. The /m switch supplies a custom message text shown alongside the option list, and /n hides the auto-generated [Y,N]? hint text that choice normally appends.
Cricket analogy: A rain-delay decision defaulting to 'continue play' if the umpires do not respond within a set time window is like choice /c YN /t 30 /d Y, auto-selecting a default after a timeout.
Case sensitivity matters most in scripts intended to accept both a shortcut key and a more mnemonic alternative, such as distinguishing lowercase y for a quick yes from uppercase Y reserved for a different, more destructive confirmation, which requires /cs. Custom messages via /m "Delete all files? " replace choice's default generic prompt, producing friendlier, context-specific interfaces without needing a separate echo line before the choice call. Combining /n (no hint text) with /m gives full control over exactly what is displayed, letting a script's prompt look identical across different choice calls throughout a longer, more polished interactive tool.
Cricket analogy: Distinguishing a quick single-run call of 'y' from a more deliberate double-run call of 'Y' shouted with extra emphasis is like choice /c yY /cs, treating the two cases as genuinely different valid inputs.
choice /c YN /n /m "Overwrite existing file? " combines a custom message with hidden hint text, producing a clean prompt that reads exactly 'Overwrite existing file?' with no extra [Y,N]? suffix appended automatically.
choice /c OPTIONSrestricts input to a fixed key set and sets errorlevel to the 1-based position of the key pressed.- Check choice results with
if errorlevel N, testing from the highest number down to the lowest to avoid false matches. /t secondscombined with/d defaultadds a timeout that auto-selects a default choice for unattended scripts.- By default choice is case-insensitive; add
/csto distinguish uppercase and lowercase keys as separate valid options. /m "text"supplies a custom prompt message;/nhides the auto-generated [Y,N]? hint text.- The default option set without /c is just Y and N, matching the classic yes/no confirmation pattern.
- choice validates keypresses automatically, rejecting invalid keys without needing manual validation logic like set /p requires.
Practice what you learned
1. What does `choice /c YNC` set errorlevel to if the user presses C?
2. Why should errorlevel checks after choice be ordered from highest to lowest?
3. What does the /t and /d combination on choice do?
4. What is the default option set if /c is omitted from a choice command?
5. What does the /cs switch do?
Was this page helpful?
You May Also Like
If/Else in Batch
Master conditional logic in Windows batch files: comparing strings and numbers, testing file existence, checking errorlevel, and chaining else branches.
Goto and Labels
Understand how batch scripts use labels and the goto command to jump execution, build subroutines with call, and structure menu-driven scripts.
Variables in Batch Scripts
Learn how Windows batch scripts store, read, and manipulate values using the set command, environment variables, delayed expansion, and substring syntax.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics