M R
M R

Reputation: 77

Shutdown batch command with time pop-up

I'm looking for a batch file code that shuts down a computer after a certain amount of time. I've got the obvious answer, which is:

shutdown -s -f -t 120

but say I want to shut down not after two minutes, but five, or ten?

Is there a way to make a .bat file that shuts the computer down after querying the user (via pop-up or otherwise) for how many minutes until the shutdown.

Now, I'm not very techy myself, but here's how I imagine this: A user runs the .bat file, and is presented with a pop-up window asking for how long until the shutdown. The user types '5', and presses 'enter'. The batch file then force-closes all open programs, and shuts down after 300 seconds (5 mins).

Additionally, can the batch file have some form of timer built in?

I've seen it done, but the cmd window is large and bulky.
(Optional: somehow incorporate an abort shutdown feature into the same batch file.)
[I know I can use shutdown -a on a separate batch file, but that's just no fun.]

Edit: Similar to the following code, except it shuts down the computer once the timer is up. Also, with a much smaller cmd box, if possible?

@ECHO off
cls
color 0B
:taskkill
echo.
echo.
echo.
echo.
echo.               
echo.               
echo.               
echo.               
echo.               
echo.
echo                                Program To Shutdown
echo.    
set /p taskkill=                      
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
:hour
color 0B
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.                                   Hours?:
echo.
set /p hour=                                      
:min
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                                   Minutes?:
echo.
set /p min=                                      
:sec
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                                   Seconds?:
echo.
set /p sec=                                      
:continue
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                           %taskkill%  Shutdown in
echo.                                                             
echo                         %hour%  Hours  %min%  Minutes  %sec%  Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit

Also, what on earth does the above code even do?

Upvotes: 0

Views: 3016

Answers (3)

iamasleep
iamasleep

Reputation: 1

Made this one for myself. I'm sure You'll like it. It's kinda janky, but it works.

@echo off

title "Shutdown Timer"

shutdown -a

:begin

set /p hours=Set hours:
set /a hours=%hours%*3600

set /p min=Set minutes:
set /a min=%min%*60

set /p sec=Set seconds:
set /a sec=%sec%

set /a timer=%hours%+%min%+%sec%

set /a currenthours=%hours%/3600
set /a currentmin=%min%/60
set /a currentsec=%sec%

echo Timer is set to %currenthours% hours, %currentmin% minutes and %currentsec% seconds

choice /c:SRA /m "S - Start Timer, R - Reset, A - Abort."
if %errorlevel% == 3 goto abort
if %errorlevel% == 2 goto reset
if %errorlevel% == 1 goto shutdownstart

:shutdownstart
echo Shutdown Timer started
shutdown /s /t %timer%

choice /c:RA /m "A - Abort Shutdown Timer, R - Reset Timer."
if %errorlevel% == 2 goto abort
if %errorlevel% == 1 goto reset

:reset
echo Resetting
shutdown -a
goto begin

:abort
echo Aborting
shutdown -a

exit
    

Also this version, if You like teh numbers.

@echo off

title "Shutdown Timer 2"

shutdown -a

:begin

set /p hours=Set hours:
set /a hours=%hours%*3600

set /p min=Set minutes:
set /a min=%min%*60

set /p sec=Set seconds:
set /a sec=%sec%

set /a timer=%hours%+%min%+%sec%

set /a currenthours=%hours%/3600
set /a currentmin=%min%/60
set /a currentsec=%sec%

echo Timer is set to %currenthours% hours, %currentmin% minutes and %currentsec% seconds

echo 1. Start Timer
echo 2. Reset
echo 3. Abort
choice /c:123 /n /m "Choose option"
if %errorlevel% == 3 goto abort
if %errorlevel% == 2 goto reset
if %errorlevel% == 1 goto shutdownstart

:shutdownstart
echo Shutdown Timer started
shutdown /s /t %timer%

echo 1. Reset
echo 2. Abort
choice /c:12 /n /m "Choose option"
if %errorlevel% == 2 goto abort
if %errorlevel% == 1 goto reset

:reset
echo Resetting
shutdown -a
goto begin

:abort
echo Aborting
shutdown -a

exit

Upvotes: 0

Robyt3
Robyt3

Reputation: 63

Here is a quick batchscript I put together. It asks the user for input in minutes and converts the input to seconds to start the shutdown. It then stops the shutdown, if any key is pressed.

custom_shutdown.bat

@echo off
title Custom Shutdown

REM Ask the user for input and convert minutes to seconds
set /P minutes="Enter shutdown delay in minutes: "
set /A seconds="%minutes%*60"

REM Start windows' shutdown command
echo Starting shutdown...
shutdown -s -f -t %seconds%

REM Cancel shutdown on keypress
echo.
echo Press any key to cancel the shutdown
pause>nul
shutdown -a
echo Shutdown canceled!

pause
exit

It is not possible to use popups to ask the user for input with the standard batch commands. Using additional applications or VBScripts you can show popups. Check out this page for more information on popup messages and inputs.

To change the size of the console window you can use the mode-command:

mode con cols=60 lines=10

There is not dedicated command to wait for some seconds but you can use the ping-command as one ping always takes one second. Because we don't need the command's output we discard it using >nul. Example code which waits for 5 seconds:

ping -n 5 localhost>nul

If you don't know what something means google helps for most batch problems. Also start with smaller programs and don't try using every function (input, math, choices etc.) in your first programs.

Upvotes: 1

Noodles
Noodles

Reputation: 2011

set /p ans=How Many Minutes to shutdown: 
set /a to=ans * 60
shutdown /s /t %to%
cls
choice /c y /m "Press Y to abort shutdown"
shutdown -a

So type for help

set /?
choice /?
shutdown /?
cls /?

If you want to loop you can use if, goto, and choice /t to implement a timer.

if /?
goto /?
choice /?

and for what commands are available (most not all)

help

Upvotes: 0

Related Questions