Advertisement
T3RRYT3RR0R

Batch Identify Time Since Last File Modified in Directory

Feb 7th, 2020
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.86 KB | None | 0 0
  1. @ECHO OFF
  2. REM Extraction of time and date information is done using substring modification within functions.
  3.  
  4.     Set "ProgLoc=%~dp0"
  5.  
  6.     CHOICE /N /C md /M "Select Date Format: (D) DDMMYYYY (M) MMDDYYYY"
  7.     IF ERRORLEVEL 2 (Set "Format=DDMM") Else (Set "Format=MMDD")
  8.  
  9. :main
  10.  
  11. REM Used to access modified variables during For loops and safeguard against incorrect input when using set /p.
  12. REM (cont) Also used to deal with directory paths and filenames with Spaces.
  13.     Setlocal EnableDelayedExpansion
  14.  
  15. REM Create a sufficiently unique filename for a temporary file and store the file path and name to a variable.
  16.     Set "FileSuffix=%random%%random%%random%"
  17.     Set "tmpLoc=%ProgLoc%tmp%FileSuffix%.sav"
  18.  
  19. REM Define directory to locate newest file in
  20.     Set CheckDir=
  21.     Set /p "CheckDir=[Drag and Drop or Enter Directory to Check:] }<"
  22.  
  23.     IF Not Defined CheckDir GOTO :main
  24.     IF /I "!CheckDir!\"=="%ProgLoc%" (
  25.         ECHO(
  26.         ECHO( This directory cannot be tested while this batch program is located within it,
  27.         ECHO( as the temporary file it creates will interfere with the results.
  28.         ECHO(
  29.         GOTO :main
  30.     )
  31.     IF /I "!CheckDir!"=="%ProgLoc%" (
  32.         ECHO(
  33.         ECHO( This directory cannot be tested while this batch program is located within it,
  34.         ECHO( as the temporary file it creates will interfere with the results.
  35.         ECHO(
  36.     )
  37.  
  38.     PUSHD "!CheckDir!"
  39.     IF ERRORLEVEL 1 (CLS & ECHO(Invalid directory & Endlocal & GOTO :main)
  40.  
  41. REM Stores the output of the Dir command to a temporary file.
  42.     dir /b/a-d/o-d/t:w>"%tmpLoc%"
  43.  
  44. REM Retrieves the topmost line of dir's output (newest file) for processing
  45.     <"%tmpLoc%" (
  46.     Set /p LatestFile=
  47.     )
  48.  
  49. REM Removes the temporary file used to store DIR commands output
  50.     DEL /Q "%tmpLoc%"
  51.  
  52. REM Calls a function to convert current date and time information into Julian date and 24 hour time for numerical comparisons.
  53.     Call :GetCurrentDT
  54.    
  55.     For %%G in ("!LatestFile!") Do (
  56. REM Ensure is a file date, not a directory
  57.         IF Not "%%~xG"=="" (
  58.             CALL :DTConvert "%%~tG" NF_Date NF_Time
  59.             Set "LastModifiedTime=!NF_Time!"
  60.             Set "LastModifiedDate=!NF_Date!"
  61.             Set "LastModifiedOn=%%~tG"
  62.             Set "LastModifiedFile=%%~G"
  63.         )
  64.     )
  65.    
  66. REM Seperate Hours from Hours / Minutes in LastModifiedTime
  67.     Set "NF_Hours=!LastModifiedTime:~,-2!"
  68.  
  69. REM Calculate difference in minutes
  70.     Set /A "Minutes_Ago=%Cur_Min% - %TimeMIN%"
  71.  
  72. REM Calculate difference in hours using the numerical 24 hour values returned by the functions
  73.     Set /A "Hours_Ago=%Cur_Hours% - %NF_Hours%
  74.     IF %Minutes_Ago% LSS 0 (
  75.         Set /A "Minutes_Ago+=60"
  76.         Set /A "Hours_Ago-=1"
  77.     )
  78.  
  79. REM Calculate the number of days difference using the coverted Julian dates
  80.     Set /A "Days_Diff=%Cur_Date% - %LastModifiedDate%"
  81.  
  82. REM Adjust both values when hours become negative.
  83.  
  84.         IF %Hours_Ago% LSS 0 (
  85.             IF Not "%Days_Diff%"=="0" (Set /A "Hours_Ago=24 + %Hours_Ago%") Else (Set "Hours_Ago=0")
  86.             IF %Days_Diff% GTR 0 Set /A "Days_Diff-=1"
  87.         )
  88.  
  89.     IF "%LastModifiedTime:~,-3%"=="" (Set "LastModifiedTime=0%LastModifiedTime%")
  90.    
  91.     CLS
  92.  
  93.     ECHO( Most recently modified file is: !LastModifiedFile!
  94.     ECHO( Modified %Days_Diff% days %Hours_Ago% hours %Minutes_Ago% minutes ago, on %LastModifiedOn:~0,10% at %LastModifiedTime%
  95.     Pause
  96.     POPD
  97.     Endlocal
  98.     Cls
  99.     GOTO :main
  100.    
  101. :DTConvert
  102.  
  103.     Setlocal
  104.     Set "tmpStr=%~1"
  105.    
  106. REM Seperate DD MM YYYY values for conversion to Julian date
  107.     Set "DateStr=%tmpStr:~0,10%"
  108.     Set "DateStr=%DateStr:/=%"
  109.  
  110.     IF "!Format!"=="DDMM" (
  111.         Set "DD=%DateStr:~0,2%"
  112.         Set "MM=%DateStr:~2,2%"
  113.     ) ELSE (
  114.         Set "MM=%DateStr:~0,2%"
  115.         Set "DD=%DateStr:~2,2%"
  116.     )
  117.  
  118. REM Remove leading zero's from DD MM values if present
  119.     Set /A DD = 100%DD% %% 100
  120.     Set /A MM = 100%MM% %% 100
  121.     Set "YYYY=%DateStr:~4,4%"
  122.    
  123. REM Modify value of parameter to convert time into 24 hour equivelant
  124.     Set "TimeStr=%tmpStr:~11,8%"
  125.     Set "TimeStr=%TimeStr::=%"
  126.     Set "TimeStr=%TimeStr: =%"
  127.     Set "TimeHR=%TimeStr:~0,2%"
  128.     Set "TimeMD=%TimeStr:~4,2%"
  129.     Set "TimeMIN=%TimeStr:~2,2%"
  130.     Set /A TimeHr = 100%TimeHr% %% 100
  131.     IF "%TimeMD%"=="PM" (IF NOT %TimeHR% EQU 12 (Set /A TimeHr+=12))
  132.     IF "%TimeMD%"=="AM" (IF %TimeHR% EQU 12 (Set "TimeHr=0"))
  133.     Set "TimeStr=%TimeHr%%TimeStr:~2,2%"
  134.  
  135. REM Call for conversion of DD MM YYYY values to Julian date
  136.     CALL :ConvertJulian %DD% %MM% %YYYY% FileDT
  137.    
  138. REM Return converted date and time values for comparison   
  139.    
  140.     (
  141.     Endlocal
  142.     Set "%~2=%FileDT%"
  143.     Set "%~3=%TimeStr%"
  144.     Set TimeMIN=%TimeMIN%
  145.     Set /A TimeMIN = 100%TimeMIN% %% 100
  146.     Exit /b
  147.     )
  148.  
  149. :ConvertJulian <DD> <MM> <YYYY> <resultVar>
  150.  
  151. REM DD and MM values MUST have leading zero's removed prior to being passed to this function as parameters
  152.    
  153.     Setlocal
  154.     Set "Day=%1"
  155.     Set "Month=%2"
  156.     Set /a "MonthCv=( %Month% - 14 ) / 12"
  157.     Set /a "YearCv=%3 + 4800"
  158.     Set /a "Julian=1461 * ( %YearCv% + %MonthCv% ) / 4 + 367 * ( %Month% - 2 -12 * %MonthCv% ) / 12 - ( 3 * ( ( %YearCv% + %MonthCv% + 100 ) / 100 ) ) / 4 + %Day% - 32075"
  159.    
  160.     (
  161.     Endlocal
  162.     Set "%~4=%Julian%"
  163.     exit /b
  164.     )
  165.    
  166.     :GetCurrentDT
  167.     Set "Cur_Date=%DATE%"
  168.     Set "Cur_Date=%Cur_Date: =%"
  169.     Set "Cur_Date=%Cur_Date:/=%"
  170.     Set "Cur_Date=%Cur_Date::=%"
  171.     Set "Cur_Date=%Cur_Date:~3,11%"
  172.     Set "Cur_YYYY=%Cur_Date:~4,4%"
  173.  
  174.     IF "%Format%"=="DDMM" (
  175.         Set "Cur_DD=!Cur_Date:~0,2!"
  176.         Set "Cur_MM=!Cur_Date:~2,2!"
  177.     ) ELSE (
  178.         Set "Cur_MM=!Cur_Date:~0,2!"
  179.         Set "Cur_DD=!Cur_Date:~2,2!"
  180.     )
  181.  
  182. REM Remove leading zero's if present.
  183.     Set /A "Cur_DD = 100%Cur_DD% %% 100"
  184.     Set /A "Cur_MM = 100%Cur_MM% %% 100"
  185.  
  186.     Call :ConvertJulian %Cur_DD% %Cur_MM% %Cur_YYYY% Cur_Date
  187.  
  188.     Set "Cur_Time=%TIME%"
  189.     Set "Cur_Time=%Cur_Time: =%"
  190.     Set "Cur_Time=%Cur_Time::=%"
  191.     Set "Cur_Time=%Cur_Time:.=%"
  192.     Set "Cur_Time=%Cur_Time:~,-4%"
  193.     IF "%Cur_Time:~,-3%"=="" (
  194.         Set "Cur_Hr=%Cur_Time:~0,1%" & Set "Cur_Min=%Cur_Time:~1,2%"
  195.     ) else (
  196.         Set "Cur_Hr=%Cur_Time:~0,2%" & Set "Cur_Min=%Cur_Time:~2,2%"
  197.     )
  198.     Set "Cur_Time=%Cur_Hr%%Cur_Min%"
  199.     Set "Cur_Hours=%Cur_Hr%"
  200.     Set /A "Cur_Min = 100%Cur_Min% %% 100"
  201.     exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement