Timeout for user decision in windows batch file

terman picture terman · Jun 28, 2013 · Viewed 7.8k times · Source

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

Answer

captcha picture captcha · Jun 28, 2013

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.