6

我编写了一个简单的 .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
4

3 回答 3

8

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

    等待用户选择一组选项中的一个。

    选择 [ /C[:]choices ] [ /N ] [ /S ] [ /T[:]c,nn ] 文本

           /C:choices 指定允许的键。
               英文版本的默认值为 YN
           /N 不显示选项?在提示字符串的末尾。
           /S 或 /CS 将选择键视为区分大小写。
              直到(包括)资源工具包版本,使用 /S。
              在 Windows 7 中使用 /CS。
           /T:c,nn nn 秒后默认选择 c。
              text 要显示的提示字符串。
 
于 2013-06-28T07:35:09.840 回答
8

这取决于您运行的 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
于 2013-06-28T07:40:26.170 回答
1

我会choice /?从提示中检查出来。

于 2013-06-28T07:35:35.727 回答