Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- REM Extraction of time and date information is done using substring modification within functions.
- Set "ProgLoc=%~dp0"
- CHOICE /N /C md /M "Select Date Format: (D) DDMMYYYY (M) MMDDYYYY"
- IF ERRORLEVEL 2 (Set "Format=DDMM") Else (Set "Format=MMDD")
- :main
- REM Used to access modified variables during For loops and safeguard against incorrect input when using set /p.
- REM (cont) Also used to deal with directory paths and filenames with Spaces.
- Setlocal EnableDelayedExpansion
- REM Create a sufficiently unique filename for a temporary file and store the file path and name to a variable.
- Set "FileSuffix=%random%%random%%random%"
- Set "tmpLoc=%ProgLoc%tmp%FileSuffix%.sav"
- REM Define directory to locate newest file in
- Set CheckDir=
- Set /p "CheckDir=[Drag and Drop or Enter Directory to Check:] }<"
- IF Not Defined CheckDir GOTO :main
- IF /I "!CheckDir!\"=="%ProgLoc%" (
- ECHO(
- ECHO( This directory cannot be tested while this batch program is located within it,
- ECHO( as the temporary file it creates will interfere with the results.
- ECHO(
- GOTO :main
- )
- IF /I "!CheckDir!"=="%ProgLoc%" (
- ECHO(
- ECHO( This directory cannot be tested while this batch program is located within it,
- ECHO( as the temporary file it creates will interfere with the results.
- ECHO(
- )
- PUSHD "!CheckDir!"
- IF ERRORLEVEL 1 (CLS & ECHO(Invalid directory & Endlocal & GOTO :main)
- REM Stores the output of the Dir command to a temporary file.
- dir /b/a-d/o-d/t:w>"%tmpLoc%"
- REM Retrieves the topmost line of dir's output (newest file) for processing
- <"%tmpLoc%" (
- Set /p LatestFile=
- )
- REM Removes the temporary file used to store DIR commands output
- DEL /Q "%tmpLoc%"
- REM Calls a function to convert current date and time information into Julian date and 24 hour time for numerical comparisons.
- Call :GetCurrentDT
- For %%G in ("!LatestFile!") Do (
- REM Ensure is a file date, not a directory
- IF Not "%%~xG"=="" (
- CALL :DTConvert "%%~tG" NF_Date NF_Time
- Set "LastModifiedTime=!NF_Time!"
- Set "LastModifiedDate=!NF_Date!"
- Set "LastModifiedOn=%%~tG"
- Set "LastModifiedFile=%%~G"
- )
- )
- REM Seperate Hours from Hours / Minutes in LastModifiedTime
- Set "NF_Hours=!LastModifiedTime:~,-2!"
- REM Calculate difference in minutes
- Set /A "Minutes_Ago=%Cur_Min% - %TimeMIN%"
- REM Calculate difference in hours using the numerical 24 hour values returned by the functions
- Set /A "Hours_Ago=%Cur_Hours% - %NF_Hours%
- IF %Minutes_Ago% LSS 0 (
- Set /A "Minutes_Ago+=60"
- Set /A "Hours_Ago-=1"
- )
- REM Calculate the number of days difference using the coverted Julian dates
- Set /A "Days_Diff=%Cur_Date% - %LastModifiedDate%"
- REM Adjust both values when hours become negative.
- IF %Hours_Ago% LSS 0 (
- IF Not "%Days_Diff%"=="0" (Set /A "Hours_Ago=24 + %Hours_Ago%") Else (Set "Hours_Ago=0")
- IF %Days_Diff% GTR 0 Set /A "Days_Diff-=1"
- )
- IF "%LastModifiedTime:~,-3%"=="" (Set "LastModifiedTime=0%LastModifiedTime%")
- CLS
- ECHO( Most recently modified file is: !LastModifiedFile!
- ECHO( Modified %Days_Diff% days %Hours_Ago% hours %Minutes_Ago% minutes ago, on %LastModifiedOn:~0,10% at %LastModifiedTime%
- Pause
- POPD
- Endlocal
- Cls
- GOTO :main
- :DTConvert
- Setlocal
- Set "tmpStr=%~1"
- REM Seperate DD MM YYYY values for conversion to Julian date
- Set "DateStr=%tmpStr:~0,10%"
- Set "DateStr=%DateStr:/=%"
- IF "!Format!"=="DDMM" (
- Set "DD=%DateStr:~0,2%"
- Set "MM=%DateStr:~2,2%"
- ) ELSE (
- Set "MM=%DateStr:~0,2%"
- Set "DD=%DateStr:~2,2%"
- )
- REM Remove leading zero's from DD MM values if present
- Set /A DD = 100%DD% %% 100
- Set /A MM = 100%MM% %% 100
- Set "YYYY=%DateStr:~4,4%"
- REM Modify value of parameter to convert time into 24 hour equivelant
- Set "TimeStr=%tmpStr:~11,8%"
- Set "TimeStr=%TimeStr::=%"
- Set "TimeStr=%TimeStr: =%"
- Set "TimeHR=%TimeStr:~0,2%"
- Set "TimeMD=%TimeStr:~4,2%"
- Set "TimeMIN=%TimeStr:~2,2%"
- Set /A TimeHr = 100%TimeHr% %% 100
- IF "%TimeMD%"=="PM" (IF NOT %TimeHR% EQU 12 (Set /A TimeHr+=12))
- IF "%TimeMD%"=="AM" (IF %TimeHR% EQU 12 (Set "TimeHr=0"))
- Set "TimeStr=%TimeHr%%TimeStr:~2,2%"
- REM Call for conversion of DD MM YYYY values to Julian date
- CALL :ConvertJulian %DD% %MM% %YYYY% FileDT
- REM Return converted date and time values for comparison
- (
- Endlocal
- Set "%~2=%FileDT%"
- Set "%~3=%TimeStr%"
- Set TimeMIN=%TimeMIN%
- Set /A TimeMIN = 100%TimeMIN% %% 100
- Exit /b
- )
- :ConvertJulian <DD> <MM> <YYYY> <resultVar>
- REM DD and MM values MUST have leading zero's removed prior to being passed to this function as parameters
- Setlocal
- Set "Day=%1"
- Set "Month=%2"
- Set /a "MonthCv=( %Month% - 14 ) / 12"
- Set /a "YearCv=%3 + 4800"
- Set /a "Julian=1461 * ( %YearCv% + %MonthCv% ) / 4 + 367 * ( %Month% - 2 -12 * %MonthCv% ) / 12 - ( 3 * ( ( %YearCv% + %MonthCv% + 100 ) / 100 ) ) / 4 + %Day% - 32075"
- (
- Endlocal
- Set "%~4=%Julian%"
- exit /b
- )
- :GetCurrentDT
- Set "Cur_Date=%DATE%"
- Set "Cur_Date=%Cur_Date: =%"
- Set "Cur_Date=%Cur_Date:/=%"
- Set "Cur_Date=%Cur_Date::=%"
- Set "Cur_Date=%Cur_Date:~3,11%"
- Set "Cur_YYYY=%Cur_Date:~4,4%"
- IF "%Format%"=="DDMM" (
- Set "Cur_DD=!Cur_Date:~0,2!"
- Set "Cur_MM=!Cur_Date:~2,2!"
- ) ELSE (
- Set "Cur_MM=!Cur_Date:~0,2!"
- Set "Cur_DD=!Cur_Date:~2,2!"
- )
- REM Remove leading zero's if present.
- Set /A "Cur_DD = 100%Cur_DD% %% 100"
- Set /A "Cur_MM = 100%Cur_MM% %% 100"
- Call :ConvertJulian %Cur_DD% %Cur_MM% %Cur_YYYY% Cur_Date
- Set "Cur_Time=%TIME%"
- Set "Cur_Time=%Cur_Time: =%"
- Set "Cur_Time=%Cur_Time::=%"
- Set "Cur_Time=%Cur_Time:.=%"
- Set "Cur_Time=%Cur_Time:~,-4%"
- IF "%Cur_Time:~,-3%"=="" (
- Set "Cur_Hr=%Cur_Time:~0,1%" & Set "Cur_Min=%Cur_Time:~1,2%"
- ) else (
- Set "Cur_Hr=%Cur_Time:~0,2%" & Set "Cur_Min=%Cur_Time:~2,2%"
- )
- Set "Cur_Time=%Cur_Hr%%Cur_Min%"
- Set "Cur_Hours=%Cur_Hr%"
- Set /A "Cur_Min = 100%Cur_Min% %% 100"
- exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement