Advertisement
BrainWaveCC

TimedStart

Jun 23rd, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.25 KB | Source Code | 0 0
  1. @REM - TimedStart.BAT (06 Jun 2024 // 23 Jun 2024): Check for Earliest Start Date
  2. @ECHO OFF
  3.  
  4.  
  5.  rem -- Initialize Environment Variables
  6. :Variables
  7.  SETLOCAL ENABLEDELAYEDEXPANSION
  8.  
  9.  rem -- Main Variables
  10.  SET #INTERVAL=28
  11.  SET #R_KEY=HKCU\Software\CustomLocation\%~n0
  12.  SET #R_VAL=NextRunTime
  13.  SET #R_TYPE=REG_SZ
  14.  SET #METHOD=REGISTRY
  15.  SET #ROOTDIR=%SystemDrive%\Temp
  16.  SET #FLAG=%#ROOTDIR%\NextDate.TXT
  17.  
  18.  
  19.  rem -- Get Date Info (using DateInfo.exe)
  20. rem -- https://www.majorgeeks.com/files/details/dateinfo.html
  21. rem -- Yes, you can do the date manipulation 100% natively, but I hated date calculations enough to write a utility to not have to deal with them any more.  LOL.  So I don't.
  22.  FOR /F "TOKENS=1" %%N IN ('DATEINFO -s -a %#INTERVAL% -f "yyyy-mm-dd" -q') DO SET "#NEXTRUN=%%N"
  23.  FOR /F "TOKENS=1-4" %%D IN ('DATEINFO -s -f "yyyy-mm-dd yyyy mm dd" -q') DO (
  24.      SET #TODAY=%%D
  25.      SET #YYYY=%%E
  26.      SET #MM=%%F
  27.      SET #DD=%%G
  28.  )
  29.  
  30.  
  31.  rem -- Check for Valid Date Entry
  32. :CheckDateEntry
  33.  FOR /F "TOKENS=3" %%R IN ('REG QUERY "%#R_KEY%" /V "%#R_VAL%" 2^>NUL') DO SET #COMPARE=%%~R
  34.  
  35.  rem -- Check for Registry Key, and if not exist, fall-back to File-based Flag
  36.  IF NOT DEFINED #COMPARE (
  37.      SET #METHOD=FILE-BASED FLAG
  38.      IF NOT EXIST "%#ROOTDIR%" MD "%#ROOTDIR%" >NUL 2>NUL
  39.      IF NOT EXIST "%#FLAG%" GOTO :DoWork
  40.  
  41.      rem -- Read Date from Flag File
  42.      rem -- Valid File Format = "Next-Run-Date: yyyy-mm-dd"
  43.      FOR /F "TOKENS=2 USEBACKQ DELIMS=|" %%D IN ("%#FLAG%") DO SET #COMPARE=%%D
  44.  )
  45.  
  46.  rem -- This next command is just there to force testing without having to change date/time on the computer.
  47. rem -- Run it as TimedStart 2024-01-01 and it will pretend that the date it read from the file is 2024-01-01 and run accordingly
  48. rem -- You will want to remove it in production
  49.  IF NOT "%~1"=="" SET #COMPARE=%~1
  50.  
  51.  rem -- If File is Empty or Not Formatted Properly, Go Do Regular Work
  52.  IF NOT DEFINED #COMPARE GOTO :DoWork
  53.  
  54.  rem -- Once you have the new date, compare it (#COMPARE) against the current date in the same format (#TODAY)
  55.  IF "%#TODAY%" LSS "%#COMPARE%" (
  56.      ECHO Not valid for execution before "%#COMPARE%" via %#METHOD%
  57.      GOTO :ExitBatch
  58.  )
  59.  
  60.  
  61.  rem -- Do Main Work
  62. :DoWork
  63.  ECHO DO WORK HERE...
  64.  ECHO DO WORK HERE...
  65.  ECHO DO WORK HERE...
  66.  ECHO DO WORK HERE...
  67.  ECHO DO WORK HERE...
  68.  ECHO DO WORK HERE...
  69.  ECHO:
  70.  
  71.  
  72.  rem -- Save Next Earliest Date for Execution (Try Registry, then fall-back to File-based Flag as Needed)
  73. :SaveNextDate
  74.  REG ADD "%#R_KEY%" /V %#R_VAL% /T %#R_TYPE% /D %#NEXTRUN% /F
  75.  
  76.  rem -- If you don't care about a fall-back to file-based Flag, then the next two commands are unnecessary
  77.  FOR /F "TOKENS=3" %%R IN ('REG QUERY "%#R_KEY%" /V "%#R_VAL%" 2^>NUL') DO SET #REG_QUERY=%%~R
  78.  IF NOT DEFINED #REG_QUERY ECHO Next-Run-Date^|%#NEXTRUN%>"%#FLAG%"
  79. rem -- You don't have to make the date as the second parameter in the FLAG file, but it helps to ensure that it was your script that wrote the file.
  80.  
  81.  rem -- The next three lines are just debug lines to verify the output for testing
  82.  ECHO NEXT EARLIEST RUN DATE ... %#NEXTRUN%
  83.  ECHO REGISTRY VALUE ........... %#REG_QUERY%
  84.  DIR "%#FLAG%" | FIND /I "%#FLAG:~-4%"
  85.  TYPE "%#FLAG%"
  86.  
  87.  
  88.  rem -- Reset Environment Variables and Exit Batch File
  89. :ExitBatch
  90.  ENDLOCAL
  91.  GOTO :EOF
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement