Windowsbatch file中的用户决策超时

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

这取决于你运行的Windows版本。 不同的运行不同的东西。

你可以尝试下面的一些:

 timeout 10 ping 0.0.0.0 -n 1 -w 10000 > nul 

如果那些失败,你总是可以选择一个简单的循环(这是如果选择的作品)

 :loop choice /t 10 /c ynr /cs /dr /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 

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

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

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

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

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