Batch: How to prompt for input and continue if timed out?

北城以北 提交于 2020-01-05 07:35:06

问题


I know the timeout /t 60 way to get a delay with automatic continue and the set /p var="prompt" for getting user input but is there any change to do both; ask and have a timeout to continue if nothing is entered? I would use it for a sort of get set screen for my looping script to change script settings.


回答1:


Take a look at choice /? to request a key and abort with a timeout.

For example:

CHOICE /T 10 /C YN /D Y

will wait 10 seconds for Y (Yes) or N (No), otherwise the default (/D) will be taken, which is Y (Yes) in this example.

To check the result (either keypressed or default value), you have to check %ERRORLEVEL%.

@echo off
cls
CHOICE /T 5 /C YN /D Y
set _e=%ERRORLEVEL%
if %_e%==1 echo Y&goto :done
if %_e%==2 echo N&goto :done

echo Error
echo %_e%

:done


来源:https://stackoverflow.com/questions/44999665/batch-how-to-prompt-for-input-and-continue-if-timed-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!