Skip to content

Instantly share code, notes, and snippets.

@do0m-nametaken
Last active January 29, 2021 13:31
Show Gist options
  • Save do0m-nametaken/5a8fe5b34e42f5811a1ad1cbe6f26f3a to your computer and use it in GitHub Desktop.
Save do0m-nametaken/5a8fe5b34e42f5811a1ad1cbe6f26f3a to your computer and use it in GitHub Desktop.
BATCH Alarm - Time alarm and timer in Batch
:: Batch ALARM by Sean
::
:: Update 24/01/2021:
:: ├─Fixed the time variable having a space for the hour value
:: └─Added a choice to exit and retry when in alarm
::
:: Update 29/01/2021:
:: ├─Made detecting input make use of the FOR command instead of extracting parts of a variable
:: ├─You can now set your custom input delimiter below
:: ├─You can now add parameters for settings
:: ├─For your hr.,min., and sec. in your input, you can now put in only 1 digit in your input if it only needs one
:: │ For example: 0:0:1 for only one second in timer
:: └─Added reasoning in the "invalid time" menu
:start
@echo off
title Batch ALARM
set isinvalid=0
set eltime=00:00:00
:: Set your custom delimiter here
:: Delimiters are for regular input and parameter input
:: Default is ":"
:: Examples:
:: 1. Custom delimiter is " " (set "inputdelim= ")
:: You can use "2 15 0" for input
::
:: 2. Custom delimiter is "-" (set "inputdelim=-")
:: You can use "2-15-0" for input
set "inputdelim="
if not defined inputdelim set "inputdelim=:"
:: How to add a parameter for settings
:: Syntax (case-insensitive): (a,Alarm/t,Timer)(custom delimiter)(Regular input)
:: Example: t:0:30:0 (Timer set to 30 minutes)
set "param=%1"
if not defined param goto:choose
if defined param for /f "tokens=1-4 delims=%inputdelim%" %%a in ("%param%") do (
set "param_1=%%a"
set "param_2=%%b"
set "param_3=%%c"
set "param_4=%%d"
if defined param_1 if defined param_2 if defined param_3 if defined param_4 set useparam=1
)
if %useparam% equ 1 if /i %param_1%==a goto:specifictime
if %useparam% equ 1 if /i %param_1%==t goto:fromnow
:choose
set useparam=0
cls
echo What type of alarm?
echo [A] Alarm - At a specific time
echo [T] Timer - Time from now
choice /c AT /n
if %ERRORLEVEL% equ 1 goto:specifictime
if %ERRORLEVEL% equ 2 goto:fromnow
:fromnow
set ifinvalid=0
set "reason1="
set "reason2="
set "reason3="
if %useparam% equ 1 if %isinvalid% neq 1 set "fninput=%param_2%%inputdelim%%param_3%%inputdelim%%param_4%" & goto:fromnow_skip
cls
echo Please specify the amount of amount of time until to start the alarm
echo Example: 1%inputdelim:~0,1%30%inputdelim:~0,1%0 (1 Hour and 30 Minutes from now)
set /p "fninput="
:fromnow_skip
for /f "tokens=1-3 delims=%inputdelim%" %%a in ("%fninput%") do (
set "hour=%%a"
set "minute=%%b"
set "second=%%c"
)
set "var1="
set "var2="
set "var3="
if defined hour for /f "delims=0123456789" %%d in ("%hour%") do set var1=%%d
if defined minute for /f "delims=0123456789" %%e in ("%minute%") do set var2=%%e
if defined second for /f "delims=0123456789" %%f in ("%second%") do set var3=%%f
if defined var1 set "ifinvalid=1" & set "reason1=is Invalid/Not numerical"
if defined var2 set "ifinvalid=1" & set "reason2=is Invalid/Not numerical"
if defined var3 set "ifinvalid=1" & set "reason3=is Invalid/Not numerical"
if not defined hour set "ifinvalid=1" & set "reason1=is Undefined"
if not defined minute set "ifinvalid=1" & set "reason2=is Undefined"
if not defined second set "ifinvalid=1" & set "reason3=is Undefined"
if %ifinvalid% equ 1 goto:inv
set digit=
set digit2=
set digit=%hour:~0,1%
set digit2=%hour:~1,1%
if %digit% equ 0 if not [%digit2%]==[] set hour=%hour:~1%
set digit=
set digit2=
set digit=%minute:~0,1%
set digit2=%minute:~1,1%
if %digit% equ 0 if not [%digit2%]==[] set minute=%minute:~1%
set digit=
set digit2=
set digit=%second:~0,1%
set digit2=%second:~1,1%
if %digit% equ 0 if not [%digit2%]==[] set second=%second:~1%
set "ctime=%time: =0%"
set "ctime=%ctime:~0,8%"
for /f "tokens=1-3 delims=:" %%a in ("%ctime%") do (
set "timehour=%%a"
set "timeminute=%%b"
set "timesecond=%%c"
)
set timesecond=%timesecond:~0,2%
set digit=
set digit=%timehour:~0,1%
if %digit% equ 0 set timehour=%timehour:~1%
set digit=
set digit=%timeminute:~0,1%
if %digit% equ 0 set timeminute=%timeminute:~1%
set digit=
set digit=%timesecond:~0,1%
if %digit% equ 0 set timesecond=%timesecond:~1%
set /a "added_hour=%hour%+%timehour%"
set /a "added_minute=%minute%+%timeminute%"
set /a "added_second=%second%+%timesecond%"
if %added_second% gtr 59 (set /a "added_second=%added_second%-60" & set /a "added_minute=%added_minute%+1")
if %added_minute% gtr 59 (set /a "added_minute=%added_minute%-60" & set /a "added_hour=%added_hour%+1")
if %added_hour% gtr 24 set /a "added_hour=%added_hour%-24"
set digit=
set digit=%added_hour:~1,1%
if not defined digit set added_hour=0%added_hour%
set digit=
set digit=%added_minute:~1,1%
if not defined digit set added_minute=0%added_minute%
set digit=
set digit=%added_second:~1,1%
if not defined digit set added_second=0%added_second%
if not defined added_hour set "ifinvalid=1" & if defined reason (set "reason=%reason%,Variable added_hour") else (set "reason=Variable added_hour")
if not defined added_minute set "ifinvalid=1" & if defined reason (set "reason=%reason%,Variable added_minute") else (set "reason=Variable added_minute")
if not defined added_second set "ifinvalid=1" & if defined reason (set "reason=%reason%,Variable added_second") else (set "reason=Variable added_second")
if %ifinvalid% equ 1 goto:inv
set "input=%added_hour%:%added_minute%:%added_second%"
set isinvalid=0
goto:loopcheck
:specifictime
set ifinvalid=0
set "reason1="
set "reason2="
set "reason3="
if %useparam% equ 1 if %isinvalid% neq 1 set "input=%param_2%%inputdelim%%param_3%%inputdelim%%param_4%" & goto:specifictime_skip
cls
echo What time will the alarm go off?
echo Example: 14%inputdelim:~0,1%45%inputdelim:~0,1%0 (2:45 P.M.)
set /p "input="
:specifictime_skip
for /f "tokens=1-3 delims=%inputdelim%" %%a in ("%input%") do (
set "hour=%%a"
set "minute=%%b"
set "second=%%c"
)
set "var1="
set "var2="
set "var3="
if defined hour for /f "delims=0123456789" %%d in ("%hour%") do set var1=%%d
if defined minute for /f "delims=0123456789" %%e in ("%minute%") do set var2=%%e
if defined second for /f "delims=0123456789" %%f in ("%second%") do set var3=%%f
if defined var1 set "ifinvalid=1" & set "reason1=is Invalid/Not numerical"
if defined var2 set "ifinvalid=1" & set "reason2=is Invalid/Not numerical"
if defined var3 set "ifinvalid=1" & set "reason3=is Invalid/Not numerical"
if not defined hour set "ifinvalid=1" & set "reason1=is Undefined"
if not defined minute set "ifinvalid=1" & set "reason2=is Undefined"
if not defined second set "ifinvalid=1" & set "reason3=is Undefined"
if %ifinvalid% equ 1 goto:inv
set digit=
set digit2=
set digit=%hour:~0,1%
set digit2=%hour:~1,1%
if %digit% equ 0 if not defined digit2 set hour=%hour:~1%0
set digit=
set digit2=
set digit=%minute:~0,1%
set digit2=%minute:~1,1%
if %digit% equ 0 if not defined digit2 set minute=%minute:~1%0
set digit=
set digit2=
set digit=%second:~0,1%
set digit2=%minute:~1,1%
if %digit% equ 0 if not defined digit2 set second=%second:~1%0
set digit=
set digit=%hour:~1,1%
if not defined digit set hour=0%hour%
set digit=
set digit=%minute:~1,1%
if not defined digit set minute=0%minute%
set digit=
set digit=%second:~1,1%
if not defined digit set second=0%second%
set "input=%hour%:%minute%:%second%"
set isinvalid=0
goto:loopcheck
:inv
set isinvalid=1
cls
echo Invalid time
echo.
echo Reasons:
if defined reason1 echo Variable hour : %reason1%
if defined reason2 echo Variable minute : %reason2%
if defined reason3 echo Variable second : %reason3%
echo.
echo Try again?
choice
if %ERRORLEVEL% equ 1 goto:choose
if %ERRORLEVEL% equ 2 exit
:loopcheck
set "ctime=%time: =0%"
cls
echo WARNING: Clicking on this window will render this window blank (nothing serious). Press any key to fix it.
echo.
echo You can minimize the window
echo The current time is %ctime:~0,8%
echo.
echo Your alarm will go off in %input%
echo.
if %input%==%ctime:~0,8% goto:alarm
echo Elapsed time:
echo %eltime%
set "elhour=%eltime:~0,2%"
set "elminute=%eltime:~3,2%"
set "elsecond=%eltime:~6,2%"
if %elhour:~0,1%==0 set "elhour=%eltime:~1,1%
if %elminute:~0,1%==0 set "elminute=%eltime:~4,1%
if %elsecond:~0,1%==0 set "elsecond=%eltime:~7,1%
set /a "elsecond+=1"
if %elsecond% gtr 59 (set /a "elsecond=%elsecond%-60" & set /a "elminute=%elminute%+1")
if %elminute% gtr 59 (set /a "elminute=%elminute%-60" & set /a "elhour=%elhour%+1")
if %elhour% gtr 24 set /a "elhour=%elhour%-24"
set digit=
set digit=%elhour:~1,1%
if not defined digit set elhour=0%elhour%
set digit=
set digit=%elminute:~1,1%
if not defined digit set elminute=0%elminute%
set digit=
set digit=%elsecond:~1,1%
if not defined digit set elsecond=0%elsecond%
set "eltime=%elhour%:%elminute%:%elsecond%"
timeout /t 1 /nobreak >nul
goto:loopcheck
:alarm
title Batch ALARM - IT'S %input%!
cls
echo IT'S %input%!
echo Press Y to exit
echo Press I for a new alarm
:noiseandchoiceloop
echo 
choice /c YIS /d S /t 1 /n >nul
if %ERRORLEVEL% equ 1 exit
if %ERRORLEVEL% equ 2 goto:choose
goto:noiseandchoiceloop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment