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

ter*_*man 6 windows cmd timeout batch-file choice

我写了一个简单的.bat文件,一次询问用户是/否问题.现在我想为它添加一个超时 - 让我们说10s.有一个简单的方法吗?

我的来源到目前为止:

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
Run Code Online (Sandbox Code Playgroud)

cap*_*cha 8

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

    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.
 


Tre*_*ton 7

这取决于您运行的Windows版本.不同的人运行不同的东西.

您可以尝试以下某些操作:

timeout 10

ping 0.0.0.0 -n 1 -w 10000 > nul
Run Code Online (Sandbox Code Playgroud)

如果那些失败,你总是可以选择一个简单的循环(即如果选择有效)

: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
Run Code Online (Sandbox Code Playgroud)