Page 1 of 1

How to compare 2 modified file timestamps?

Posted: 28 Feb 2024 10:54
by tobwz
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?

Re: How to compare 2 modified file timestamps?

Posted: 28 Feb 2024 13:12
by Squashman
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"

Re: How to compare 2 modified file timestamps?

Posted: 28 Feb 2024 13:34
by Squashman
I should have known we answered this question before.
Compare timestamps of two files (seconds too)

Re: How to compare 2 modified file timestamps?

Posted: 29 Feb 2024 01:32
by tobwz
Thank you

Re: How to compare 2 modified file timestamps?

Posted: 03 Mar 2024 12:39
by Aacini
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

Re: How to compare 2 modified file timestamps?

Posted: 04 Mar 2024 01:45
by tobwz
@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

Re: How to compare 2 modified file timestamps?

Posted: 25 Mar 2024 19:09
by Batcher
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