In a cmd window, I want to prompt the user for a set of choices (denoted by letters), one being the default.
The user can key in a letter or hit Enter
, in which case the default choice will be selected.
I also want to specify a timeout, so the default choice is auto-selected after a given number of seconds of inactivity.
The built-in choice
command comes close:
> choice /C YN /T 5 /D Y /M "The message"
This takes only valid choices ( /C YN
) and auto-defaults to 'Y' (/D Y
) after 5 seconds (/T 5
)
... but it strangely does not allow hitting Enter
for the default choice.
The other, more verbose option I found permits hitting 'Enter' for the default choice:
:ch
set sel=Y
REM Hitting Enter does not modify 'sel'
set /p sel=The message, [Y/N]?
if %sel% == Y goto Yes
if %sel% == N goto No
echo Invalid choice '%sel%', try again.
goto ch
...but I don't know how to make this one auto-select the default after a while.