How to compare 2 modified file timestamps?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

How to compare 2 modified file timestamps?

#1 Post by tobwz » 28 Feb 2024 10:54

Assume I have 2 files aaaa.dat and bbbb.dat

How can I find out from DOS batch file which of the two files has an older last modified timestamp?

Squashman
Expert
Posts: 4485
Joined: 23 Dec 2011 13:59

Re: How to compare 2 modified file timestamps?

#2 Post by Squashman » 28 Feb 2024 13:12

This will not get seconds. Only has minute precision.

Code: Select all

for %%G in ("aaaa.dat") do for /f "tokens=1-5 delims=/: " %%H IN ("%%~tG") do set "aaaats=%%J%%H%%I%%K%%L"

Squashman
Expert
Posts: 4485
Joined: 23 Dec 2011 13:59

Re: How to compare 2 modified file timestamps?

#3 Post by Squashman » 28 Feb 2024 13:34

I should have known we answered this question before.
Compare timestamps of two files (seconds too)

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to compare 2 modified file timestamps?

#4 Post by tobwz » 29 Feb 2024 01:32

Thank you

Aacini
Expert
Posts: 1910
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to compare 2 modified file timestamps?

#5 Post by Aacini » 03 Mar 2024 12:39

Do you want to get the timestamps of two files and compare them, OR you just want to know which file is the last modified one?

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "files=aaaa.dat;bbbb.dat"
set "older="
for /F "delims=" %%a in ('dir *.dat /O:-D') do (
   if not defined older (
      if "!files:%%a=!" neq "%files%" set "older=%%a"
   )
)
echo Older file: %%a
If you want to use another time, add the /T: switch in DIR command

Antonio

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to compare 2 modified file timestamps?

#6 Post by tobwz » 04 Mar 2024 01:45

@Aacini:

Thank you for your code although it does not work but the idea is good.
I adjusted the code a little:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "files=aaaa.dat;bbbb.dat"
set "older="
for /F "delims=" %%a in ('dir *.dat /B /O:-D') do (
   echo current a=%%a
   if "!files:%%a=!" neq "%files%" set "older=%%a"
)
echo Older file: %older%
pause

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: How to compare 2 modified file timestamps?

#7 Post by Batcher » 25 Mar 2024 19:09

V1.bat

Code: Select all

@echo off
set "File1=D:\\Test\\1.txt"
set "File2=D:\\Test\\2.txt"
for /f "tokens=2 delims==" %%i in ('wmic DataFile where "Name='%File1%'" get LastModified /value ^| findstr "="') do (
    for %%j in ("%%i") do (
        set "ModiTime1=%%~j"
    )
)
for /f "tokens=2 delims==" %%i in ('wmic DataFile where "Name='%File2%'" get LastModified /value ^| findstr "="') do (
    for %%j in ("%%i") do (
        set "ModiTime2=%%~j"
    )
)
if "%ModiTime1%" lss "%ModiTime2%" (
    echo Older file is "%File1%"
) else if "%ModiTime1%" gtr "%ModiTime2%" (
    echo Older file is "%File2%"
) else (
    echo Same age
)
pause

Post Reply