KubosKube

Shutdown Later

Aug 10th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.78 KB | Software | 0 0
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3.  
  4. rem Initialize and go to start
  5.  
  6. set title=Shutdown Later
  7. set version=2.0
  8.  
  9. set splash1= %title% v%version%
  10. set splash2=  UI by KubosKube
  11.  
  12. title %splash1%
  13.  
  14. set preset1= 1m
  15. set preset2= 2m
  16. set preset3= 3m
  17. set preset4= 5m
  18. set preset5=10m
  19. set preset6=20m
  20. set preset7=30m
  21. set preset8= 1h
  22. set preset9= 2h
  23. set preset10= 3h
  24.  
  25. goto :start
  26.  
  27.  
  28.  
  29. rem Splash Text
  30. :splash
  31. echo %splash1%
  32. echo %splash2%
  33. echo %splash3%
  34. goto :eof
  35.  
  36.  
  37.  
  38.  
  39. rem Display options
  40. :start
  41.  
  42. cls
  43. echo.
  44. echo ! WORK IN PROGRESS !
  45. echo.
  46. call :splash
  47. echo.
  48. echo  Choose a preset time offset, or provide your own timestamp and offset.
  49. echo   1 ............ %preset1%
  50. echo   2 ............ %preset2%
  51. echo   3 ............ %preset3%
  52. echo   4 ............ %preset4%
  53. echo   5 ............ %preset5%
  54. echo   6 ............ %preset6%
  55. echo   7 ............ %preset7%
  56. echo   8 ............ %preset8%
  57. echo   9 ............ %preset9%
  58. echo   10 ........... %preset10%
  59. echo.
  60. echo   +01:30:00 .... shutdown in 1h30m
  61. echo   01:30:00 ..... shutdown at 1:30am
  62. echo.
  63. echo   help ......... Show examples of how to use non-preset times.
  64. echo   changelog .... Show changes from previous versions to current version.
  65. echo.
  66. echo.
  67.  
  68. echo  Enter a preset number or a custom input...
  69. set /p input=
  70.  
  71. if not defined input goto :start
  72.  
  73. if %input% == changelog goto :changelog
  74. if %input% == help goto :help
  75.  
  76. rem Early jump to Custom Input
  77. if not %input% == %input:+=% goto :custom
  78.  
  79. rem Check for Preset input
  80. set loop=10
  81. :inputLoop
  82. if %input% EQU %loop% goto presets
  83. set /a loop-=1
  84. if %loop% GTR 0 goto inputLoop
  85.  
  86. rem No preset given, go to Custom Input
  87. goto :custom
  88.  
  89.  
  90.  
  91. rem Parse Preset Inputs
  92. :presets
  93.  
  94. call :debug "Preset input provided"
  95.  
  96. if %input% EQU 1 set /a offset=1*60
  97. if %input% EQU 2 set /a offset=2*60
  98. if %input% EQU 3 set /a offset=3*60
  99. if %input% EQU 4 set /a offset=5*60
  100. if %input% EQU 5 set /a offset=10*60
  101. if %input% EQU 6 set /a offset=20*60
  102. if %input% EQU 7 set /a offset=30*60
  103. if %input% EQU 8 set /a offset=1*60*60
  104. if %input% EQU 9 set /a offset=2*60*60
  105. if %input% EQU 10 set /a offset=3*60*60
  106.  
  107. goto :execute
  108.  
  109.  
  110.  
  111. rem Parse Custom Input
  112. :custom
  113.  
  114. call :debug "Custom input provided"
  115.  
  116. if %input% == %input:+=% (
  117.     call :debug "Mode: timestamp"
  118.     set time_set=%input%
  119. ) else (
  120.     call :debug "Mode: offset"
  121.     set time_offset=%input:*+=%
  122.     call set "time_set=%%input:+!time_offset!=%%"
  123. )
  124.  
  125. call :debug "input = %input%   time_set = %time_set%   time_offset = %time_offset%"
  126.  
  127. if defined time_set (
  128.  
  129.     call :debug "Entering timestamp calculations"
  130.    
  131.     call :tokenize %time_set%
  132.     for /f "tokens=1-3" %%a in ("!tokens!") do ( set hours=00%%a& set minutes=00%%b& set seconds=00%%c)
  133.  
  134.     set time_stamp=!hours:~-2!:!minutes:~-2!:!seconds:~-2!
  135.  
  136.     call :debug "Time Stamp = !time_stamp!"
  137.    
  138.     call :convertTime %time%
  139.     set /a time_now=!convertedTime! + 0
  140.     call :convertTime !time_stamp!
  141.     set /a time_then=!convertedTime! + 0
  142.  
  143.     set /a "time_delta=(!time_then!-!time_now!) + 0"
  144.    
  145.     call :debug "time_now = !time_now!   time_then = !time_then!   time_delta = !time_delta!"
  146.  
  147.     if !time_delta! LSS 0 (
  148.         set /a "time_delta= !time_delta! + (1*24*60*60)"
  149.        
  150.         call :debug "Corrected time_delta = !time_delta!"
  151.     )
  152. )
  153.  
  154. call :debug "This line is just before time_offset calculations. time_offset = %time_offset%"
  155.  
  156. if defined time_offset (
  157.  
  158.     call :debug "Entering timestamp calculations"
  159.    
  160.     call :tokenize +%time_offset%
  161.    
  162.     for /f "tokens=1-4" %%a in ("!tokens!") do ( set /a days=%%d + 0& set /a hours=%%c + 0& set /a minutes=%%b + 0& set /a seconds=%%a + 0 )
  163.  
  164.     set offset_nominal=!days! days, !hours! hours, !minutes! minutes, !seconds! seconds
  165.  
  166.     call :debug "Offset = !offset_nominal!"
  167.  
  168.     call :debug "about to run the command: set /a offset_in_seconds=((!days! * 24 * 60 * 60) + (!hours! * 60 * 60) + (!minutes! * 60) + !seconds!)"
  169.     set /a offset_in_seconds=((!days! * 24 * 60 * 60) + (!hours! * 60 * 60) + (!minutes! * 60) + !seconds!)
  170.  
  171.     call :debug "offset_in_seconds = !offset_in_seconds!"
  172. )
  173.  
  174. call :debug "this line is immediate after offset calculations"
  175.  
  176. if not defined time_delta set time_delta=0
  177. if not defined offset_in_seconds set offset_in_seconds=0
  178.  
  179. set /a "time_to_wait_in_seconds=(%time_delta% + %offset_in_seconds%)"
  180.  
  181. call :debug "adding time_delta %time_delta% with %offset_in_seconds% to get total wait time %time_to_wait_in_seconds%"
  182.  
  183. goto :execute
  184.  
  185. call :error "Oops, something seems to have gone catestrophically and horrendously insanely wrong. Try again?"
  186.  
  187. rem This will likely never ever happen right here, but just in case the code pointer escapes, here's a shortcut to the end.
  188. goto :eof
  189.  
  190.  
  191.  
  192. rem Tokenize Argument
  193. :tokenize
  194.  
  195. for /f "tokens=1-4 delims=:." %%a in ("%1") do ( set tokens=%%a %%b %%c %%d )
  196.  
  197. call :debug "Tokenize %1 -> %tokens%"
  198.  
  199. if not "!tokens!" == "!tokens:+=!" (
  200.    
  201.     call :debug "'!tokens!' not equal to '!tokens:+=!'"
  202.    
  203.     set reverse=
  204.    
  205.     for %%a in (!tokens!) do (
  206.         set reverse=%%a !reverse!
  207.         shift
  208.     )
  209.    
  210.     set tokens=!reverse:+=!
  211.     call :debug "Reversed tokens for offset use. %1 -> !tokens!"
  212. )
  213.  
  214. goto :eof
  215.  
  216.  
  217.  
  218. rem Time Format -> Seconds
  219. :convertTime
  220.  
  221. for /f "tokens=1-3 delims=:.," %%a in ("%1") do (
  222.  
  223.     call :debug "Converting Time: arg1 %%1   deliminated a %%a   b %%b   c %%c"
  224.    
  225.     set /a "convertedTime=((%%a * 60 * 60) + (%%b * 60) + %%c)"
  226. )
  227.  
  228. call :debug "Convert Time %1 -> %convertedTime%"
  229.  
  230. goto :eof
  231.  
  232.  
  233.  
  234. rem Debug
  235. :debug
  236.  
  237. echo.
  238. echo %1
  239. echo.
  240.  
  241. pause
  242.  
  243. goto :eof
  244.  
  245.  
  246.  
  247. rem Error Reporter
  248. :error
  249. echo.
  250. echo.
  251. echo  error
  252. echo  %1
  253. echo.
  254. echo.
  255.  
  256. pause
  257.  
  258. goto :start
  259.  
  260.  
  261.  
  262. rem Execution of Shut Down
  263. :execute
  264.  
  265. cls
  266. echo.
  267. call :splash
  268. echo.
  269.  
  270. call :debug "Entering Execution. Adding timestamp %timestamp% and %offset_in_seconds% to get total wait time %time_to_wait_in_seconds%"
  271.  
  272. echo  Shutting down in %time_to_wait_in_seconds% seconds
  273. :: echo  such as %time_stamp% and %offset_nominal%
  274. echo.
  275. echo  Closing this window will interrupt the process safely.
  276. echo  Have a nice day!
  277. echo.
  278.  
  279. timeout %time_to_wait_in_seconds% /nobreak
  280.  
  281. ::if defined offset shutdown /s /t 5
  282.  
  283. echo That totally worked.
  284.  
  285. pause
  286.  
  287. goto :eof
  288.  
  289.  
  290.  
  291. rem Help
  292. :help
  293. cls
  294. echo.
  295. call :splash
  296. echo.
  297. echo -----
  298. echo.
  299. echo  This script can understand two types of custom input.
  300. echo.
  301. echo    "Set" Time
  302. echo    and
  303. echo    "Offset" Time
  304. echo.
  305. echo  It differentiates between the two based on the presense/location of a "+" symbol.
  306. echo.
  307. echo  A custom input of "12:00:00" will translate to 12:00pm.
  308. echo  A custom input of "+12:00:00" will translate to mean twelve hours from time of input.
  309. echo.
  310. echo  Both can be used together.
  311. echo.
  312. echo  A custom input of "12:00:00+12:00:00" will translate to
  313. echo  Twelve hours after 12:00pm.
  314. echo.
  315. echo.
  316. echo  page 1 / 5
  317. echo.
  318.  
  319. pause
  320.  
  321. echo -----
  322. echo.
  323. echo  Colons can be replaced with Decimals freely.
  324. echo  This is to make entry on the 10-key Number Pad easier.
  325. echo.
  326. echo  Also, the number of digits is not monitored.
  327. echo.
  328. echo  For example, the following custom inputs would all be parsed identically:
  329. echo.
  330. echo    12:00:00
  331. echo    12.00.00
  332. echo.
  333. echo    12:00000:00
  334. echo    12.0.0
  335. echo.
  336. echo.
  337. echo  page 2 / 5
  338. echo.
  339.  
  340. pause
  341.  
  342. echo -----
  343. echo.
  344. echo  For "Set" Time, the hours and minutes are required.
  345. echo  If you do not include a placeholder for seconds, it will be assumed to be zero.
  346. echo.
  347. echo  The following two examples are parsed identically:
  348. echo.
  349. echo    12:00:00
  350. echo    12:00
  351. echo.
  352. echo.
  353. echo  page 3 / 5
  354. echo.
  355.  
  356. pause
  357.  
  358. echo -----
  359. echo.
  360. echo  For "Offset" Time, parsing assumes you input seconds, and builds up from there.
  361. echo  "Offset" Time also can accept up to a number of days to delay for.
  362. echo.
  363. echo  The following table shows a handful of inputs and their effect to demonstrate these points.
  364. echo.
  365. echo    +30 ........... plus 30 seconds
  366. echo    +1:30 ......... plus 1 minute 30 seconds
  367. echo    +1:30:00 ...... plus 1 hour 30 minutes
  368. echo    +1:00:00:00 ... plus 1 day
  369. echo.
  370. echo.
  371. echo  page 4 / 5
  372. echo.
  373.  
  374. pause
  375.  
  376. echo -----
  377. echo.
  378. echo  This script only has limited error handling.
  379. echo.
  380. echo  Always double-check the final screen after input shows the correct value.
  381. echo.
  382. echo.
  383. echo  page 5 / 5
  384. echo.
  385. echo  Continuing now will lead back to the main menu.
  386. echo.
  387.  
  388. pause
  389.  
  390. goto :start
  391.  
  392.  
  393.  
  394. rem Changelog
  395. :changelog
  396. cls
  397. echo.
  398. call :splash
  399. echo.
  400. echo  v2.0
  401. echo   + Re-made UI
  402. echo   + Added ability to shut down at a set time
  403. echo   + Added splash text to every screen
  404. echo   + Added a help menu
  405. echo   + Added a changelog
  406. echo.
  407. echo   - Removed the entirety of v1.0
  408. echo.
  409. echo.
  410. echo  v1.0
  411. echo   + Added the ability to shut down after a delay
  412. echo.
  413.  
  414. pause
  415. goto :start
  416.  
  417. :: You've made it to the end of the document, congratulations.
  418. :: Please tell me about my redundant remarks next to every label.
Tags: batch
Advertisement
Add Comment
Please, Sign In to add comment