Timeout for user decision in windows batch file

后端 未结 3 1361
一整个雨季
一整个雨季 2020-12-18 05:25

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

相关标签:
3条回答
  • 2020-12-18 05:47

    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.
     
    0 讨论(0)
  • 2020-12-18 05:48

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

    0 讨论(0)
  • 2020-12-18 06:00

    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
    
    0 讨论(0)
提交回复
热议问题