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?
How to compare 2 modified file timestamps?
Moderator: DosItHelp
Re: How to compare 2 modified file timestamps?
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?
I should have known we answered this question before.
Compare timestamps of two files (seconds too)
Compare timestamps of two files (seconds too)
Re: How to compare 2 modified file timestamps?
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?
If you want to use another time, add the /T: switch in DIR command
Antonio
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
Antonio
Re: How to compare 2 modified file timestamps?
@Aacini:
Thank you for your code although it does not work but the idea is good.
I adjusted the code a little:
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?
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