Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout for user decision in windows batch file

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
like image 764
terman Avatar asked Jun 28 '13 07:06

terman


People also ask

How do I create a timeout in a batch file?

Timeout with the parameter /NOBREAK If we take the example from before and run that in a BATCH file: timeout /t 60 then while waiting those 60 seconds, you are actually able to break the timeout by pressing any key on your keyboard. To prevent this we simply add the parameter /NOBREAK to the end of it.

How do you wait 5 seconds in CMD?

Syntax TIMEOUT delay [/nobreak] Key delay Delay in seconds (between -1 and 100000) to wait before continuing. The value -1 causes the computer to wait indefinitely for a keystroke (like the PAUSE command) /nobreak Ignore user key strokes.

Is there a wait command in batch?

The Sleep/Wait Command is a very useful command that is used to pause for a set amount of time while a batch script is being executed. To put it another way, this command makes it easier to run a command for a set amount of time.

How do you put a batch file to sleep?

The correct way to sleep in a batch file is to use the timeout command, introduced in Windows 2000.


3 Answers

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
like image 155
TrevorPeyton Avatar answered Oct 27 '22 20:10

TrevorPeyton


You can try the choice /T:c,nn command, if you are on Vista or later:

    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.
 
like image 42
captcha Avatar answered Oct 27 '22 20:10

captcha


I'd check out choice /? from the prompt.

like image 1
Magoo Avatar answered Oct 27 '22 22:10

Magoo