TechKnow

Author Topic: [BAT] GetFileTimeDiff  (Read 747 times)

0 Members and 1 Guest are viewing this topic.

Offline HcH

  • TechKnow Owner, Ethical Hacker
  • Administrator
  • Uber Member
  • *****
  • Posts: 9936
  • Karma: +2284/-111
[BAT] GetFileTimeDiff
« on: August 30, 2016, 12:20:51 PM »
For work I needed a simple file time comparison tool, files older than 4 minutes should not be present in a dir. Utilizing some for loop together with this script allows one to check for files in a directory and report which files are older than 4 minutes. You can easily change the time difference in minutes by changing the DIFFERENCEMINUTES veriable.

@echo off

set DIFFERENCEMINUTES=4

REM STEP 1. GET CURRENT TIME
REM ------------------------
set cuu=%TIME:~0,2%
set cmm=%TIME:~3,2%


REM STEP2. GET FILE TIME
REM --------------------
if [%1]==[] echo ERROR: No file specified to get date and time from^! && PAUSE && EXIT
if not [%1]==[] set FILENAME=%1
for /F "tokens=1,2" %%i in ('dir %FILENAME% ^| findstr /i %FILENAME%') do (
set FILEDATE=%%i
set FILETIME=%%j
) >NUL
echo.&&echo %FILENAME%^: %FILEDATE% %FILETIME%
set fuu=%FILETIME:~0,2%
set fmm=%FILETIME:~3,2%


REM STEP3. SUBSTRACT DIFFERENCEMINUTES WITH CURRENT TIME
REM ----------------------------------------------------
set TSUBSTRACT=%DIFFERENCEMINUTES%
set uu=%TIME:~0,2%
set mm=%TIME:~3,2%
rem echo Current minutes: %MM%
set /a MM2=%MM%-%TSUBSTRACT%
if %MM2% LSS 0 set /a UU=%UU%-1
if %MM2% LSS 0 set mm2=%mm2:~1,2% && set /a mm2=%mm2%+60
if %MM2% LEQ 9 set MM2=0%MM2%
if %MM2% LSS 0 set /a MM2=%MM2%+59
set TSUBSTR=%UU%:%MM2%
REM echo. && echo Current time minus %TSUBSTRACT% minutes: %UU%:%MM2%, set to variable %%TSUBSTR%% ^(%TSUBSTR%^). && echo.


REM STEP4. DIFFERENTIATE
REM --------------------
echo Current time     : %cuu% ^(CUU^) %cmm% ^(CMM^)
echo DifferenceMinutes: %DIFFERENCEMINUTES%
echo DifferenceSubstr : %UU% ^(DUU^) %MM2% ^(DMM^)
echo File time        : %fuu% ^(FUU^) %fmm% ^(FMM^)
echo Endcheck#1   : %UU%%MM2% LSS %FUU%%FMM% echo OK
echo Endcheck#2   : %UU%%MM2% GTR %FUU%%FMM% echo NOT OK

if %UU%%MM2% LSS %FUU%%FMM% echo OK
if %UU%%MM2% GTR %FUU%%FMM% echo NOT OK

echo.
pause

:EOF