computing
  • 2

Solved Comparing Two File Dates In CMD

  • 2

Hello all

hope you can help.

Using a batch file, I have recently been asked to compare two file dates in a particular folder. Lets say the files names are F1.txt and mail.log.

If mail.log date is prior to F1.txt date then I want the batch file command to run another bat file, wait 20 seconds and then compare two other files within another folder…

____________________________________
The logics is like this:

IF mail.log date < F1.txt date in C:\TEST1 then run Run.bat ELSE
Wait 20 seconds
IF mail.log date < F2.txt date in C:\TEST2 then run Run.bat ELSE
Wait 20 seconds….
____________________________________

There are about 10 of these folders.
-I also do not want to run the Run.bat if the file dates in that folder are the -same, only if the mail.log file less than the F. files
-I also want to remove the time portion of the date

I have looked at some code like this:

SET FILE1=mail.log
SET FILE2=F1.txt
FOR /F %%i IN (‘DIR /B /O:D %FILE1% %FILE2%’) DO SET NEWEST=%%i
ECHO %NEWEST%

but I cannot fit what I have together
Please can someone help

Thankyou

Share

1 Answer

  1. Wow, I just tested it and there were several problems. That’s what I get for trying to write it in my head.

    Try this:

    @echo off & setlocal enabledelayedexpansion
    set #=0
    set $=mail.log
    set _=F
    :_loop
    set /a #+=1
    pushd c:\TEST%#%
    for /f "tokens=*" %%i in (
    '2^>nul dir /b/od !$! !_!%#%.txt'
    ) do (
    if {%%i}=={mail.log} (
    call run.bat
    popd
    goto :_loop
    ) else (
    timeout /t 20 /nobreak
    popd
    goto :_loop
    )
    )
    

    Run it from the root of the C: drive.

    Tony

    • 0