超时在Windows批处理文件的用户决定 [英] Timeout for user decision in windows batch file

查看:268
本文介绍了超时在Windows批处理文件的用户决定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,要求用户是/在一个点毫无疑问,一个简单的.bat文件。现在,我想添加一个超时 - 可以说10S - 它。有没有一种简单的方法来做到这一点?

我的来源至今:

  SET / p ANSWER =你希望它(Y / N)?
IF / I {%ANSWER%} == {Y} GOTO:是
IF / I {%ANSWER%} {==是} GOTO:是
GOTO:无:是
@echo是的,它会做。
GOTO:继续:没有
@echo不,这不会发生。
GOTO:继续:继续
@echo而就我们去


解决方案

这取决于Windows版本正在运行。不同的人执行不同的东西。

您可以尝试以下一些:

  10超时平0.0.0.0 -n 1 -w 10000> NUL

如果这些失败,你可以随时与选择一个简单的循环(也就是说,如果选择的作品)去

 :循环
选择/吨10 / C YNR / CS / D R / M你想它(Y / N)?
如果ERRORLEVEL 3转到:循环
如果错误级别页转到:无
如果ERRORLEVEL 1页转到:是:是
@echo是的,它会做。
GOTO:继续:没有
@echo不,这不会发生。
GOTO:继续:继续
@echo而就我们去

I wrote a simple .bat file that asks the user a yes/ no question at one point. Now I want to add a timeout - lets say 10s - to it. Is there an easy way to do it?

My source so far:

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

解决方案

This depends on version of windows your running. Different ones run different things.

You can try some of the following:

timeout 10

ping 0.0.0.0 -n 1 -w 10000 > nul

If those fail you could always go with a simple loop with choice (That is if choice works)

: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

这篇关于超时在Windows批处理文件的用户决定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆