1

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alis
  • 11
  • 1
  • 6
  • 2
    Would it be acceptable to have a regular `timeout /t 30` followed by an immediate shutdown using `shutdown /s /t 0`? – SomethingDark Feb 27 '21 at 11:28
  • If you want to **trigger** a shutdown process in 30 seconds time, that's fine, and there's nothing wrong with giving your end user 30 seconds to close their own choice of open documents and applications first. But I see no reason to `/f`orce a shutdown, as this does not care whether the end user has managed to successfully, and safely, save their work and close those applications, before potentially losing their unsaved data. It would be better to provide the end user with message informing them of the ensuing shutdown, and then after that specific delay, do so without `/f`orcing it. – Compo Feb 27 '21 at 12:22
  • I doubt very much that adding a countdown helps the situation either. The end user should be busy trying to save their work etc. and, will at that point, no longer be able to read a countdown timer in a no longer in focus, and probably, now behind other windows, window for it to have any effect, other than introducing a panic feeling. – Compo Feb 27 '21 at 12:28

2 Answers2

3

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
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • This is Brilliant can you add other 2 choice too? the Logoff and Reboot if possible – Alis Feb 28 '21 at 13:28
  • See the edit in the answer for an example of using the timer as a callable function. – T3RR0R Feb 28 '21 at 15:53
  • You the BEST Billiant well done and very Impressive – Alis Feb 28 '21 at 19:22
  • how to closed ? i mean i happy with it and want to close – Alis Feb 28 '21 at 19:26
  • If iok may i ask how to also add into the batch like Press Enter or anything to Restart now straight away if done early before timer out – Alis Mar 01 '21 at 09:25
  • That is already included with the `[N]o Wait` Option – T3RR0R Mar 01 '21 at 09:26
  • Lolsss Thank You and again thank you, check – Alis Mar 01 '21 at 10:05
  • [See here](https://stackoverflow.com/help/someone-answers) for info on how to accept an answer – T3RR0R Mar 01 '21 at 10:47
  • can ask? if it ok is there a way for cmd to open on second monitor – Alis Mar 01 '21 at 12:27
  • i a put it here https://stackoverflow.com/questions/66413118/batch-how-get-batch-cmd-to-popup-on-the-second-monitor-not-the-default-monitor I found the Mark so going to say thank you again Thank.... – Alis Mar 01 '21 at 12:35
1

PsShutdown

You could use Mark Rusinovich's psshutdown which does exactly what you want:

  • initiates shutdown after specified amount of time
  • displays countdown
  • allows user to cancel with a cancel button

It is free to use.

Commandline: psshutdown -k -f -t 30 -c

  • -k shuts down the computer (ACPI power off)
  • -f forces programs to terminate if necessary
  • -t 30 introduces a delay of 30 seconds before shutdown
  • -c allows user to cancel shutdown with Cancel button

psshutdown window with countdown and 'cancel' button ( Screenshot from superuser.com )

Choice

Another option would be to call choice /t 1 multiple times:

@echo off

set /a "t=30"
:loop
set /a "t-=1"
if "%t%" == "0" goto timedout
cls
choice /T 1 /C sc /N /D s /M "Shutdown in %t% seconds, press c to cancel: "
if not "%errorlevel%" == "1" goto cancelled 
goto loop

:cancelled
echo You cancelled shutdown
pause
goto :eof

:timedout
echo Shutting down
shutdown /s /f /t 1
pause
goto :eof
  • set /a "t=30" sets variable t to 30
  • set /a "t-=1" substracts 1 from t
  • if "%t%" == "0" goto timedout initiates shutdown when t reaches zero
  • cls clears previous text from screen
  • choice is timeout that knows if it was cancelled but without printing time
    • /T 1 - timeout in 1 second (to update the message)
    • /C sc - choice 1 is s, choice 2 is c
    • /N - don't display [s,c] in the end
    • /D s - default option on 1-second-timeout is s
    • /M "Shutdown in %t% ..." - display message with current value of variable t

Please note that shutdown /s /f /t NNN may not behave the same as shutdown /s /f without time, so I included /t 1 just in case.

Shutdown /a

It may be advantageous to actually initiate shutdown before the countdown, but allow the user to abort it:

  • Do the countdown as described in previous section ("Choice").

    • You could get away with just plain timeout command with a few more seconds in hopes that the batch script is aborted by shutdown before the time runs out but it would be very confusing and unreliable, so this is not recommended
  • Before beginning the countdown (:loop) start the shutdown like this: shutdown /s /f /t 30

  • If timed out, do nothing (remove shutdown /s /f /t 1 after timedout:)

  • If user cancelled (right after :cancelled), abort it with: shutdown /a

Jack White
  • 896
  • 5
  • 7
  • @Alis - the second option (using `choice`) does exactly what you're looking for. – SomethingDark Feb 27 '21 at 18:09
  • Oh whoops, sorry, last part is fixed to indicate to use the `choice` part also. PsShutdown also displays the countdown right in its own window (which replaces the normal `you are about to be signed out` window) and allows to cancel it with a button also in its window. – Jack White Feb 27 '21 at 20:19
  • Also added correct commandline for `psshutdown` with screenshot copied from superuser.com - forgot to do that when first answering, sorry – Jack White Feb 27 '21 at 20:28