Windows批处理文件中用户决策超时

czq61nw1  于 2023-01-06  发布在  Windows
关注(0)|答案(3)|浏览(143)

我写了一个简单的.bat文件,在某个点上询问用户是/否问题。现在我想给它添加一个超时--比如说10秒。有没有简单的方法可以做到这一点?
我目前的消息来源是:

SET /P ANSWER=Do you want it (Y/N)?
IF /i {%ANSWER%}=={y} GOTO :yes
IF /i {%ANSWER%}=={yes} GOTO :yes
GOTO :no

:yes
@echo Yeah, it will be done.
GOTO :continue

:no
@echo Nope, it will not happen.
GOTO :continue

:continue
@echo And on we go
k3fezbri

k3fezbri1#

这取决于你运行的windows版本。不同的运行不同的东西。
您可以尝试以下操作:

timeout 10

ping 0.0.0.0 -n 1 -w 10000 > nul

如果这些都失败了,你可以用一个简单的循环选择(如果选择工作)

:loop
choice /t 10 /c ynr /cs /d r /m "Do you want it (Y/N)?"
if errorlevel 3 goto :loop
if errorlevel 2 goto :no
if errorlevel 1 goto :yes

:yes
@echo Yeah, it will be done.
GOTO :continue

:no
@echo Nope, it will not happen.
GOTO :continue

:continue
@echo And on we go
t2a7ltrp

t2a7ltrp2#

如果您使用的是Vista或更高版本,可以尝试choice /T:c,nn命令:

Waits for the user to choose one of a set of choices.

    CHOICE  [ /C[:]choices ]  [ /N ]  [ /S ]  [ /T[:]c,nn ] text

           /C:choices    Specifies allowable keys.
               Default for English versions is YN
           /N            Do not display choices an ? at end of prompt string.
           /S or /CS     Treat choice keys as case sensitive.
              Up to (and including) the Resource Kit versions, use /S.
              In Windows 7 use /CS.
           /T:c,nn      Default choice to c after nn seconds.
              text      Prompt string to display.
w8ntj3qf

w8ntj3qf3#

我会从提示符处 checkout choice /?

相关问题