Skip to content

Instantly share code, notes, and snippets.

@scarpa27
Created January 27, 2023 16:52
Show Gist options
  • Save scarpa27/da5fd7c8c11ba780d7db87203969472b to your computer and use it in GitHub Desktop.
Save scarpa27/da5fd7c8c11ba780d7db87203969472b to your computer and use it in GitHub Desktop.
Shutdown script prompts timeout in minutes. Loops to start if string entered. Can be stopped at any moment. After windows shutdown initiated, restarting and inputing "no" stops shutdown.
@echo off
setlocal enableextensions enabledelayedexpansion
echo %time:~0,-3%
echo If timed out and shuttdown has started,
echo start me again and input: no
GOTO first
:first
::read user input
echo.
set /P "in=In how many minutes do you want automatic shutdown? "
set /A "ts=in"
if !ts! EQU 0 (
if !in! EQU 0 (
GOTO second
) else (
if "%in%" == "no" (
shutdown -a
exit
)
echo %in% is not a number
echo. & GOTO :first
)
) else (
GOTO second
)
:second
set /A "ts*=60"
::start time
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
echo Press anything to abort
timeout %ts% >nul
::end time
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
::if end after midnight
if !end! LSS !start! (
set /A "end+=8640000"
)
set /A "delta=1+(end-start)/100"
if !delta! LSS !ts! (
echo Shuttdown aborted
goto first
)
shutdown -s -f -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment