Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal EnableDelayedExpansion
- goto :initialize
- :changelog
- call :operation_tracker "Changelog"
- cls
- echo.
- call :splash
- echo.
- echo v2.0.1
- echo + Added description to splash text for what the script does
- echo + Added functionality for negative numbers in offset time
- echo + Changed logic for selecting preset input in code
- echo + Fixed a bug regarding octal numbers
- echo + Fixed a bug regarding wait times longer than 99,999 seconds
- echo v2.0.0
- echo + Re-made UI
- echo + Added ability to shut down at a set time
- echo + Added splash text to every screen
- echo + Added a help menu
- echo + Added a changelog
- echo.
- echo v1.0
- echo + Added the ability to shut down after a delay
- echo.
- pause
- goto :start
- :initialize
- call :operation_tracker "Initialize"
- set "title=Shutdown Later"
- set "version=2.0.1"
- set "splash1= %title% v%version%"
- set "splash2= An overly-complicated script from KubosKube to you, free of charge."
- set "splash3= Shuts down your computer at a later point in time."
- title %splash1%
- set "preset1= 1m"
- set "preset2= 2m"
- set "preset3= 3m"
- set "preset4= 5m"
- set "preset5=10m"
- set "preset6=20m"
- set "preset7=30m"
- set "preset8= 1h"
- set "preset9= 2h"
- set "preset0= 3h"
- goto :start
- rem Splash Text
- :splash
- echo %splash1%
- echo %splash2%
- echo %splash3%
- goto :eof
- :start
- call :operation_tracker "Start"
- cls
- echo.
- call :splash
- echo.
- echo Choose a preset time offset, or provide your own timestamp and offset.
- echo 1 ............ %preset1%
- echo 2 ............ %preset2%
- echo 3 ............ %preset3%
- echo 4 ............ %preset4%
- echo 5 ............ %preset5%
- echo 6 ............ %preset6%
- echo 7 ............ %preset7%
- echo 8 ............ %preset8%
- echo 9 ............ %preset9%
- echo 0 ............ %preset0%
- echo.
- echo +01:30:00 .... shutdown in 1h30m
- echo 01:30:00 ..... shutdown at 1:30am
- echo.
- echo help ......... Show examples of how to use non-preset times.
- echo changelog .... Show changes from previous versions to current version.
- echo.
- echo.
- echo Enter a number preset or a custom input...
- set /p "user_input="
- ::set "user_input=08.00+30.00"
- if not defined user_input goto :start
- if %user_input% == changelog goto :changelog
- if %user_input% == help goto :help
- call :convertTime %time%
- set /a "time_now=%convertTime_output% + 0"
- rem call :debug "time_now=%time_now%"
- rem Early jump to Custom Input
- if not %user_input% == %user_input:+=% goto :custom
- rem Check for Preset input
- set "loop=9"
- :inputLoop
- if "%user_input%" == "%loop%" goto :presets
- set /a "loop-=1"
- if %loop% GTR -1 goto inputLoop
- rem No preset given, go to Custom Input
- goto :custom
- :presets
- call :operation_tracker "Presets"
- rem call :debug "Preset input provided"
- if %user_input% EQU 1 set /a "offset_delay_seconds=1*60"
- if %user_input% EQU 2 set /a "offset_delay_seconds=2*60"
- if %user_input% EQU 3 set /a "offset_delay_seconds=3*60"
- if %user_input% EQU 4 set /a "offset_delay_seconds=5*60"
- if %user_input% EQU 5 set /a "offset_delay_seconds=10*60"
- if %user_input% EQU 6 set /a "offset_delay_seconds=20*60"
- if %user_input% EQU 7 set /a "offset_delay_seconds=30*60"
- if %user_input% EQU 8 set /a "offset_delay_seconds=1*60*60"
- if %user_input% EQU 9 set /a "offset_delay_seconds=2*60*60"
- if %user_input% EQU 0 set /a "offset_delay_seconds=3*60*60"
- goto :execute
- :custom
- call :operation_tracker "Custom"
- rem call :debug "Custom input provided"
- set "time_set="
- set "time_offset="
- if %user_input% == %user_input:+=% (
- set "mode=set"
- set "time_set=%user_input%"
- ) else (
- set "mode=offset"
- set "time_offset=%user_input:*+=%"
- call set "time_set=%%user_input:+!time_offset!=%%"
- )
- rem call :debug "user_input = %user_input% time_set = %time_set% time_offset = %time_offset%"
- if defined time_set (
- call :operation_tracker "Time_Set Calculations"
- rem call :debug "Entering time_set calculations"
- call :tokenize %time_set%
- for /f "tokens=1-3" %%a in ("!tokenize_output!") do (
- set /a "time_set_hours=%%a + 0"
- set /a "time_set_minutes=%%b + 0"
- set /a "time_set_seconds=%%c + 0"
- )
- set "time_set_hours_00=00!time_set_hours!"
- set "time_set_minutes_00=00!time_set_minutes!"
- set "time_set_seconds_00=00!time_set_seconds!"
- set "time_set_full=!time_set_hours!.!time_set_minutes!.!time_set_seconds!"
- set "time_set_full_00=!time_set_hours_00:~-2!:!time_set_minutes_00:~-2!:!time_set_seconds_00:~-2!"
- rem call :debug "Time Stamp = !time_set_full!"
- call :convertTime !time_set_full!
- set /a "time_then=!convertTime_output! + 0"
- set /a "set_delay_seconds=(!time_then!-!time_now!) + 0"
- rem call :debug "time_now = !time_now! time_then = !time_then! set_delay_seconds = !set_delay_seconds!"
- if !set_delay_seconds! LSS 0 (
- set /a "set_delay_seconds= !set_delay_seconds! + (1*24*60*60)"
- rem call :debug "Corrected set_delay_seconds = !set_delay_seconds!"
- )
- ) else ( set "set_delay_seconds=0" )
- if defined time_offset (
- call :operation_tracker "Time_Offset Calculations"
- rem call :debug "Entering timestamp calculations"
- call :tokenize +%time_offset%
- for /f "tokens=1-4" %%a in ("!tokenize_output!") do (
- set /a "time_offset_days=%%d + 0"
- set /a "time_offset_hours=%%c + 0"
- set /a "time_offset_minutes=%%b + 0"
- set /a "time_offset_seconds=%%a + 0"
- )
- set offset_nominal=!time_offset_days! days, !time_offset_hours! hours, !time_offset_minutes! minutes, !time_offset_seconds! seconds
- rem call :debug "Offset = !offset_nominal!"
- rem call :debug "about to run the command: set /a offset_delay_seconds=((!time_offset_days! * 24 * 60 * 60) + (!time_offset_hours! * 60 * 60) + (!time_offset_minutes! * 60) + !time_offset_seconds!)"
- set /a "offset_delay_seconds=((!time_offset_days! * 24 * 60 * 60) + (!time_offset_hours! * 60 * 60) + (!time_offset_minutes! * 60) + !time_offset_seconds!)"
- rem call :debug "offset_delay_seconds = !offset_delay_seconds!"
- ) else ( set "delay_offset=0" )
- rem call :debug "adding set_delay_seconds %set_delay_seconds% with %offset_delay_seconds% to get total wait time %total_wait_seconds%"
- goto :execute
- call :error "Oops, something seems to have gone catestrophically and horrendously insanely wrong. Try again?"
- rem Failsafe
- goto :eof
- :tokenize
- call :operation_tracker "Tokenize"
- set "tokenize_input=%1"
- if not "%tokenize_input%" == "%tokenize_input:+=%" ( set "token_type=offset" ) else ( set "token_type=set" )
- for /f "tokens=1-4 delims=:.,+" %%a in ("%tokenize_input%") do (
- if not "%%a" == "" ( call :strip_lead_zeros %%a & set "token_a=!strip_lead_zeros_output!" )
- if not "%%b" == "" ( call :strip_lead_zeros %%b & set "token_b=!strip_lead_zeros_output!" )
- if not "%%c" == "" ( call :strip_lead_zeros %%c & set "token_c=!strip_lead_zeros_output!" )
- if not "%%d" == "" ( call :strip_lead_zeros %%d & set "token_d=!strip_lead_zeros_output!" )
- )
- set "tokenize_output=%token_a% %token_b% %token_c% %token_d%"
- if "%token_type%" == "offset" (
- set "reverse="
- for %%a in (%tokenize_output%) do (
- set "reverse=%%a !reverse!"
- shift
- )
- set "tokenize_output=!reverse!"
- )
- set "token_a="
- set "token_b="
- set "token_c="
- set "token_d="
- goto :eof
- :strip_lead_zeros
- call :operation_tracker "Strip Lead Zeros"
- set "strip_lead_zeros_input=%1"
- rem call :debug "strip_lead_zeros_input=%strip_lead_zeros_input% multiplier=%multiplier% strip_lead_zeros_output=%strip_lead_zeros_output%
- if "%strip_lead_zeros_input%" == "%strip_lead_zeros_input:-=%" ( set /a "multiplier=1" ) else ( set /a "multiplier=-1" )
- rem call :debug "strip_lead_zeros_input=%strip_lead_zeros_input% multiplier=%multiplier% strip_lead_zeros_output=%strip_lead_zeros_output%
- set /a "strip_lead_zeros_output=1%strip_lead_zeros_input:-=% - ( 11%strip_lead_zeros_input:-=% - 1%strip_lead_zeros_input:-=% ) / 10"
- rem call :debug "strip_lead_zeros_input=%strip_lead_zeros_input% multiplier=%multiplier% strip_lead_zeros_output=%strip_lead_zeros_output%
- set /a "strip_lead_zeros_output= %strip_lead_zeros_output% * %multiplier%"
- rem call :debug "strip_lead_zeros_input=%strip_lead_zeros_input% multiplier=%multiplier% strip_lead_zeros_output=%strip_lead_zeros_output%"
- goto :eof
- rem Time Format -> Seconds
- :convertTime
- call :operation_tracker "Convert Time"
- for /f "tokens=1-3 delims=:.," %%a in ("%1") do (
- rem call :debug "Converting Time: arg1 %%1 deliminated a %%a b %%b c %%c"
- set /a "convertTime_output=(( %%a * 60 * 60) + ( (%%b * 60) + %%c +0))"
- rem call :debug "Convert Time %1 -> (( %%a * 60 * 60) + ( (%%b * 60) + %%c +0)) = !convertTime_output!"
- )
- goto :eof
- rem Debug
- :debug
- if not defined debug_count ( set "debug_count=0" )
- set /a "debug_count+=1"
- echo.
- echo Debug %debug_count%
- echo %1
- echo.
- pause
- goto :eof
- :operation_tracker
- set "operation_step=%operation_step% -> %1"
- set "operation_step=%operation_step:"=%"
- rem call :debug "%operation_step%"
- goto :eof
- rem Nominalize ( well, kinda, not really )
- :nominalize
- call :operation_tracker "Nominalize"
- set /a "seconds_per_day=(24*60*60)"
- set /a "seconds_per_hour=(60*60)"
- set /a "seconds_per_minute=(60)"
- set /a "number_of_days=(%1 / %seconds_per_day%)", "remainder=(%1 %% %seconds_per_day%)"
- set /a "number_of_hours=(%remainder% / %seconds_per_hour%)", "remainder=(%remainder% %% %seconds_per_hour%)"
- set /a "number_of_minutes=(%remainder% / %seconds_per_minute%)", "number_of_seconds=(%remainder% %% %seconds_per_minute%)"
- rem call :debug "Nominalize from %1 seconds to days %number_of_days% hours %number_of_hours% minutes %number_of_minutes% seconds %number_of_seconds%"
- goto :eof
- rem Execution of Shut Down
- :execute
- call :operation_tracker "Execute"
- if not defined set_delay_seconds set set_delay_seconds=0
- if not defined offset_delay_seconds set offset_delay_seconds=0
- set /a "total_wait_seconds=(%set_delay_seconds% + %offset_delay_seconds%)"
- cls
- echo.
- call :splash
- echo.
- rem call :debug "Entering Execution. Adding timestamp %timestamp% and %offset_delay_seconds% to get total wait time %total_wait_seconds%"
- call :nominalize %total_wait_seconds%
- echo Shutdown will begin in %number_of_days% days, %number_of_hours% hours, %number_of_minutes% minutes, and %number_of_seconds% seconds.
- echo.
- set /a "shutdown_time=%time_now% + %total_wait_seconds%"
- call :nominalize %shutdown_time%
- set number_of_minutes=00%number_of_minutes%
- set number_of_seconds=00%number_of_seconds%
- echo That will be in %number_of_days% days at %number_of_hours%:%number_of_minutes:~-2%:%number_of_seconds:~-2%
- echo.
- echo Closing this window will interrupt the process safely.
- echo Have a nice day!
- echo.
- if %total_wait_seconds% LSS 0 (
- cls
- echo.
- call :splash
- echo.
- echo Whoops, the time you specified is in the past. Try again.
- echo.
- pause
- goto :start
- )
- call :wait
- shutdown /s /t 5
- echo.
- echo Goodbye
- echo.
- timeout 10 /nobreak
- call :error "Script reached conclusion, but PC has yet to shut down."
- goto :eof
- :wait
- set /a "timeout_limit=24 * 60 * 60"
- if %total_wait_seconds% GTR %timeout_limit% (
- timeout %timeout_limit% /nobreak
- ) else (
- timeout %total_wait_seconds% /nobreak
- )
- set /a "total_wait_seconds-=%timeout_limit%"
- if %total_wait_seconds% GTR 0 ( goto :wait )
- goto :eof
- :help
- cls
- echo.
- call :splash
- echo.
- echo -----
- echo.
- echo This script can understand two types of custom input.
- echo.
- echo "Set" Time
- echo and
- echo "Offset" Time
- echo.
- echo It differentiates between the two based on the presense/location of a "+" symbol.
- echo.
- echo A custom input of "12:00:00" will translate to 12:00pm.
- echo A custom input of "+12:00:00" will translate to mean twelve hours from time of input.
- echo.
- echo Both can be used together.
- echo.
- echo A custom input of "12:00:00+12:00:00" will translate to
- echo Twelve hours after 12:00pm.
- echo.
- echo.
- echo page 1 / 6
- echo.
- pause
- echo -----
- echo.
- echo Colons can be replaced with Decimals freely.
- echo This is to make entry on the 10-key Number Pad easier.
- echo.
- echo Also, the number of digits is not monitored.
- echo.
- echo For example, the following custom inputs would all be parsed identically:
- echo.
- echo 12:00:00
- echo 12.00.00
- echo.
- echo 12:00000:00
- echo 12.0.0
- echo.
- echo.
- echo page 2 / 6
- echo.
- pause
- echo -----
- echo.
- echo For "Set" Time, the hours and minutes are required.
- echo If you do not include a placeholder for seconds, it will be assumed to be zero.
- echo.
- echo The following two examples are parsed identically:
- echo.
- echo 12:00:00
- echo 12:00
- echo.
- echo.
- echo page 3 / 6
- echo.
- pause
- echo -----
- echo.
- echo For "Offset" Time, parsing assumes you input seconds, and builds up from there.
- echo "Offset" Time also can accept up to a number of days to delay for.
- echo.
- echo The following table shows a handful of inputs and their effect to demonstrate these points.
- echo.
- echo +30 ........... plus 30 seconds
- echo +1:30 ......... plus 1 minute 30 seconds
- echo +1:30:00 ...... plus 1 hour 30 minutes
- echo +1:00:00:00 ... plus 1 day
- echo.
- echo.
- echo page 4 / 6
- echo.
- pause
- echo -----
- echo.
- echo For "Offset" Time, parsing accounts for negative numbers.
- echo While you cannot have an over-all sum of less than zero seconds,
- echo this may make inputting some amounts of time easier.
- echo.
- echo The following table shows a handful of inputs and their effect to demonstrate these points.
- echo.
- echo +30:-30 ............. plus 30 minutes minus 30 seconds, or plus 29 minutes and 30 seconds
- echo +1:-15:00 ........... plus 1 hours minus 15 minutes, or plus 45 minutes
- echo 12:00:00+-1:00:00 ... 12:00 PM minus 1 hour, or 11:00 AM.
- echo.
- echo.
- echo page 5 / 6
- echo.
- pause
- echo -----
- echo.
- echo This script only has limited error handling.
- echo.
- echo Always double-check the final screen after input shows the correct value.
- echo.
- echo.
- echo page 6 / 6
- echo.
- echo Continuing now will lead back to the main menu.
- echo.
- pause
- goto :start
- rem Error Reporter
- :error
- cls
- echo.
- set convertTime_output
- set multiplier
- set number_of_days
- set number_of_hours
- set number_of_minutes
- set number_of_seconds
- set offset_delay_seconds
- set offset_nominal
- set preset0
- set preset1
- set preset2
- set preset3
- set preset4
- set preset5
- set preset6
- set preset7
- set preset8
- set preset9
- set remainder
- set reverse
- set seconds_per_day
- set seconds_per_hour
- set seconds_per_minute
- set set_delay_seconds
- set shutdown_time
- set splash1
- set splash2
- set splash3
- set strip_lead_zeros_input
- set strip_lead_zeros_output
- set timeout_limit
- set time_now
- set time_offset
- set time_offset_days
- set time_offset_hours
- set time_offset_minutes
- set time_offset_seconds
- set title
- set tokenize_input
- set tokenize_output
- set token_type
- set total_wait_seconds
- set user_input
- set version
- echo.
- echo.
- echo An error has occured.
- echo %1
- echo.
- echo The error was in:
- echo " %operation_step:~4% "
- echo.
- echo A list of all variables made up to this point is listed above.
- echo.
- echo If you wish to report this error,
- echo copy the entirety of the text in this window and send it to KubosKube someway, somehow.
- echo You can copy it to your clipboard, or take a screenshot.
- echo.
- echo The script can be exited anytime. Thank you for your patience.
- echo.
- echo Pressing any key will exit the script while this window is in focus.
- echo.
- pause
- exit
Advertisement
Add Comment
Please, Sign In to add comment