1

Hi I need help as I'm new to BatchScript. I need to check if any file/folder within a directory has been modified in the last 15 mins.

Here's my logic :

  1. Find last modified date
  2. Find current date
  3. Find if difference between these two is 15 mins. I'm able to do the 1st 2 steps.I'm stuck with the third Please help me to find the time difference between these 2 dates. Or if there's a better/easier logic.

Here is my code:

@echo off
for /f %a in (' dir "D:\BatchFiles" /od/b/s/t') do set Date1= %~ta
echo The most recently created file is %Date1%
@echo off
for /f "delims=" %i in ('time /t') do set output=%i
@echo off
SET Date2= %DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4% %output%
echo The current date is %Date2%
PAUSE
java
  • 11
  • 3
  • for date/time math there is neither an easy nor a good solution with pure batch (although it's possible). Consider Powershell, Java or VBA for that part. – Stephan Aug 10 '16 at 15:05

1 Answers1

0

Here is a pure batch file that returns all files in a given directory of a given age, with the following restrictions:

  • dates are not resolved correctly, so if a file has been modified in the previous month, it might not be included in the result erroneously; in case the maximum age is to be defined in terms of days, a forfiles solution might be more reasonable;
  • files that have both characters ) and , in their paths will be missing in the output; this is because of a design flaw of the wmic command, which is used to retrieve locale-independent date/time information of the last modification of files;

To use the script -- let us call it max-aged-files.bat --, provide command line arguments like this:

max-aged-files.bat 15*60 "D:\BatchFiles"

The first argument is the maximal age of a file in terms of seconds; simple arithmetic expressions like 15*60 are understood. The second argument is the location and/or file pattern to apply for searching files; you can state a directory path like "D:\BatchFiles" here, or a file pattern like "*.bat", or a comination like "D:\BatchFiles\*.bat"; it you omit it, the current directory is used.

Here is the code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem /* Please regard that this script cannot handle dates correctly!
rem    so if current date and file date are in different months it fails! */

rem // Retrieve and prepare arguments:
set "MAXAGE=%~1" & rem // (maximum age of files in terms of seconds)
if defined MAXAGE (set /A "MAXAGE=%MAXAGE%+0") else (
    >&2 echo ERROR: maximum age not specified! & exit /B 1
)
if %MAXAGE% GEQ 86400 (>&2 echo ERROR: maximum age exceeds range! & exit /B 1)
shift /1
set "LOCATION=%~1"
set "ATTR=%~a1"
set "ATTR=%ATTR:~,1%"
if not defined LOCATION (set "LOCATION=.\*.*") else (
    if "%ATTR%"=="d" set "LOCATION=%LOCATION%\*.*"
)
rem /* Gather current date/time in standardised format
rem    [like: `YYYYMMDDHHMMSS.UUUUUU+ZZZ`]: */
for /F "delims=" %%I in ('wmic OS GET LocalDateTime') do (
    for /F "delims=" %%J in ("%%I") do set "CURRTIME=%%J"
)
rem // Extract `YYYYMMSS` portion from current date/time:
set "CURRDATE=%CURRTIME:~,8%"
rem // Extract `HHMMSS` portion from current date/time only:
for /F "delims=." %%T in ("%CURRTIME:~8%") do (
    set "CURRTIME=%%T"
)
rem // Loop through all files at given location:
for %%F in ("%LOCATION%") do (
    set "ITEM=%%~fF"
    setlocal EnableDelayedExpansion
    rem // Gather file date/time in standardised format:
    for /F "delims=" %%E in ('
        2^> nul wmic DataFile WHERE Name^="!ITEM:\=\\!" GET LastModified ^|^| ^
        2^> nul wmic DataFile WHERE ^(Name^="!ITEM:\=\\!"^) GET LastModified
    ') do (
        for /F "delims=" %%F in ("%%E") do set "FILETIME=%%F"
    )
    rem // Extract `YYYYMMSS` portion from file date/time:
    set "FILEDATE=!FILETIME:~,8!"
    rem // Extract `HHMMSS` portion from file date/time:
    for /F "delims=." %%T in ("!FILETIME:~8!") do (
        set "FILETIME=%%T"
    )
    rem // Compute date difference between file and current date:
    set /A "DATEDIFF=CURRDATE-FILEDATE"
    rem // Continue processing only if date difference is zero or one:
    if !DATEDIFF! GEQ 0 if !DATEDIFF! LEQ 1 (
        rem // Convert date difference to seconds:
        set /A "DATEDIFF*=240000"
        rem // Compute time difference, regarding also date difference:
        set /A "TIMEDIFF=DATEDIFF+1!CURRTIME!-1!FILETIME!"
        rem // Pad time difference to consist of 6 digits [like `HHMMSS`]:
        set "TIMEDIFF=000000!TIMEDIFF!" & set "TIMEDIFF=!TIMEDIFF:~-6!"
        rem // Convert time difference to seconds:
        set /A "TIMEDIFF=1!TIMEDIFF:~-2!-100+60*(1!TIMEDIFF:~-4,-2!-100+60*(1!TIMEDIFF:~-6,-4!-100))"
        rem // Return item if 
        if !TIMEDIFF! LEQ %MAXAGE% (
            echo(!ITEM!
        )
    )
    endlocal
)

endlocal
exit /B
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99