Search code examples
batch-filecountdown

Batch countdown but WITH showing countdown


I do know it can Batch timeout 30 etc but how to do showing countdown for

I saw here: http://www.trytoprogram.com/batch-file/shutdown-commands/ BOTTOM Page "Batch file program to shutdown, reboot, hibernate, and logoff the computer"

/t 0 is no good ever i did do /t 30 it not showing countdown

Any idea how to show the countdown like the: timeout 30 29 28 27 etc


Solution

  • A timer can be effected by using a for /L loop for the number of seconds, and a for /F loop executing a choice command with a /T 1 Delay with a /D default that performs no action.

    Other options can be built into the For /F Choice loop to allow Cancellation of the shutdown, resetting of the countdown, or immediate shutdown:

     @Echo off
    :Reset
     For /L %%i in (30 -1 1)Do (
      Cls
      Echo Shutting down in : %%i Seconds [C]ancel [S]hutdown Now [R]eset Timer
      For /F "Delims=" %%G in ('Choice /T 1 /N /C:CSRW /D W')Do (
       If %%G==R Goto :Reset
       If %%G==C Goto :Eof
       If %%G==S Shutdown /SG /T 0
      )
     )
     Shutdown /SG /T 1
    

    EDIT:

    Example of modifying the above for use as a function called from a menu.

    @Echo off
    :Menu
     Echo [E]xit [L]ogoff [R]estart [S]hutdown
     For /F "Delims=" %%G in ('Choice /N /C:SRLE')Do (
      If %%G==S Call :Timer "Shutdown /SG /T 1" "Shutting Down"
      If %%G==R Call :Timer "Shutdown /R /T 1" "Restarting"
      If %%G==L Call :Timer "Shutdown /L" "Logging Off"
      If %%G==E Exit /B
     )
    :# On Cancel Command Selection:
    Goto :Menu
    :# Function With timer
    :Timer [Command] [Prompt]
     For /L %%i in (30 -1 1)Do (
      Cls
      Echo %~2 in : %%i Seconds [C]ancel [R]estart Timer [N]o wait
      For /F "Delims=" %%G in ('Choice /T 1 /N /C:CRNW /D W')Do (
       If %%G==R Goto :Timer
       If %%G==C Goto :Eof
       If %%G==N %~1
      )
     )
     %~1