6

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
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
terman
  • 303
  • 4
  • 12

3 Answers3

9

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
TrevorPeyton
  • 629
  • 3
  • 10
  • 22
  • 1
    @terman Yep, one thing you should note about the choice command. As you have probably found out by now it goes on keystroke and if you don't enter one of the choices then it will make a loud beeping noise. I really don't like that but I found out the hard way by it scaring the heck out of me. – TrevorPeyton Jun 28 '13 at 18:26
  • thanks for the advice. I didn't notice so far since I have no speaker on my development machine. – terman Jun 30 '13 at 17:48
  • When you use ERRORLEVEL parameters in a batch program, list them in decreasing order. – William Leader Nov 09 '14 at 01:40
  • @WilliamLeader Why do you have to do that? (Not doubting you, I've never heard that before just want an explanation) – TrevorPeyton Nov 09 '14 at 02:32
  • Its listed in the output of choice.exe /? NOTE: The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order. – William Leader Nov 10 '14 at 16:05
  • Also, this behavior is documented here: http://support.microsoft.com/kb/39585, and yes this particular document is way out of date, but the behavior in batch files persists for backwards compatibility reasons. – William Leader Nov 10 '14 at 16:07
  • The documentation for Choice is now located at: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/choice – MSeverin Apr 17 '20 at 17:34
8

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.
 
captcha
  • 3,756
  • 12
  • 21
1

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

Magoo
  • 77,302
  • 8
  • 62
  • 84