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
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.
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.
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.
The correct way to sleep in a batch file is to use the timeout command, introduced in Windows 2000.
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
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.
I'd check out choice /?
from the prompt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With