How to Get User Input with a Timeout in Batch Script
Interactive batch scripts often need to pause and wait for the user to make a decision. The PAUSE command works but waits forever. The SET /P command is great for input but also waits indefinitely. For automated or time-sensitive scripts, you often need to ask a question but proceed with a default action if no one responds within a certain time.
This guide will teach you how to use the built-in CHOICE command to create a prompt that accepts specific keys and automatically continues after a timeout. This is the standard and most reliable method for creating non-blocking user prompts in a batch script.
The Core Command: CHOICE
The CHOICE command is a powerful, built-in utility designed specifically for this task. It presents the user with a prompt and a set of allowed keys, waits for a keypress, and then sets the %ERRORLEVEL% variable to indicate which key was pressed.
Its most important features for this task are:
- It can be configured to accept only specific keys (e.g.,
YandN). - It has a built-in timeout feature that will automatically make a default choice if the user doesn't respond.
Basic Example: A Simple Timed Prompt
This script asks the user a question. If they don't answer within 10 seconds, it will automatically default to "No".
@ECHO OFF
ECHO --- Timed Prompt ---
ECHO You have 10 seconds to answer.
CHOICE /C YN /T 10 /D N /M "Do you want to proceed?"
REM The logic to handle the choice goes here...
The output presented to the user is with the message and a prompt showing the allowed choices. The countdown timer is also visible.
--- Timed Prompt ---
You have 10 seconds to answer.
Do you want to proceed? [Y,N]?
If the user presses Y or N within 10 seconds, the command ends immediately. If they do nothing, after 10 seconds the command will automatically select N and continue.
How to Use the User's Selection (%ERRORLEVEL%)
The CHOICE command communicates the user's selection by setting the %ERRORLEVEL% variable to the index of the chosen key in the /C list.
- The first choice (
Yin/C YN) corresponds to anERRORLEVELof 1. - The second choice (
N) corresponds to anERRORLEVELof 2. - And so on.
If the timeout occurs, %ERRORLEVEL% is set to the index of the default choice. If the user presses Ctrl+C to abort, %ERRORLEVEL% is 0.
An example:
@ECHO OFF
ECHO You have 10 seconds to decide.
CHOICE /C YN /T 10 /D N /M "Do you want to format the drive?"
ECHO.
REM The check must be done in descending order with the classic syntax.
IF ERRORLEVEL 2 (
ECHO You chose 'N'. Operation cancelled.
GOTO :End
)
IF ERRORLEVEL 1 (
ECHO You chose 'Y'. Proceeding with format...
GOTO :End
)
:End
The IF ERRORLEVEL N syntax means "if the error level is N or greater", which is why we must check from highest to lowest.
Key CHOICE Parameters Explained
/C <Choices>: Specifies the set of Choices the user can press. Example:/C YNCfor Yes, No, Cancel./T <Timeout>: Sets the Timeout in seconds. After this many seconds, the default choice is made./D <DefaultChoice>: Specifies the Default choice to be used if the timeout occurs. This must be one of the characters from your/Clist./N: No prompt. This hides the[Y,N]?part of the prompt, which is useful if your message (/M) already includes the options./M "<Message>": The Message string to display to the user.
Common Pitfalls and How to Solve Them
Problem: The Prompt is Case-Sensitive
By default, /C YN will only accept uppercase Y and N. If a user types a lowercase y, it will be ignored.
Solution: Include Both Cases in the Choices
The simplest solution is to include both upper and lower case letters in your choice list. The %ERRORLEVEL% will be set according to the position in this list.
@ECHO OFF
REM Choices: y, Y, n, N
REM Indexes: 1, 2, 3, 4
CHOICE /C yYnN /T 10 /D N /M "Proceed? (y/n)"
IF ERRORLEVEL 3 GOTO :ChoiceNo
IF ERRORLEVEL 1 GOTO :ChoiceYes
:ChoiceYes
ECHO You chose YES.
GOTO :End
:ChoiceNo
ECHO You chose NO.
:End
Here, we check for an ERRORLEVEL of 3 or greater for "No", which covers both n and N.
Problem: Hiding the Prompt and Countdown
For a cleaner interface, you might not want the [Y,N]? text to appear.
The Solution: Use the /N Switch
The /N switch suppresses the default prompt text, allowing you to create a fully custom message with the /M switch.
CHOICE /N /C YN /T 10 /D N /M "Press Y to continue or wait 10 seconds to cancel..."
Practical Example: An Unattended Script with an Abort Option
This script is designed to run automatically. It gives the user a short 5-second window to press C to cancel a potentially destructive operation before it proceeds automatically.
@ECHO OFF
SETLOCAL
ECHO --- Automated Server Reboot Script ---
ECHO.
ECHO WARNING: This script will reboot the server in 15 seconds.
ECHO To abort this operation, press 'C' within the next 15 seconds.
ECHO.
CHOICE /C C /T 15 /D X /N
REM Choice C is the 1st in the list, so its index is 1.
REM If the timeout occurs, the default 'X' is chosen, index 2.
REM If the user hits Ctrl+C, the ERRORLEVEL is 0.
IF %ERRORLEVEL% EQU 1 (
ECHO [ABORTED] Reboot has been cancelled by the user.
GOTO :End
)
ECHO Timeout reached or another key was pressed.
ECHO Proceeding with server reboot NOW.
REM (The actual reboot command would go here)
REM shutdown /r /t 0
:End
ECHO Script finished.
ENDLOCAL
Conclusion
The CHOICE command is the definitive tool for creating interactive prompts that won't halt your script indefinitely. It provides a simple and reliable way to get user input under a time limit.
Key takeaways for using it effectively:
- Use the
/Tswitch to set the timeout in seconds. - Use the
/Dswitch to specify the default choice on timeout. - Check the
%ERRORLEVEL%variable to determine which key was pressed, remembering that it corresponds to the 1-based index of the choice. - For non-interactive scripts, the combination of
/Tand/Dis essential for providing an "opt-out" or "abort" window before a default action is taken.