marcss
marcss

Reputation: 253

Get the modified date of two files and compare to do a task?

I've got two files and I need to get the modified date, I've found the solution this way:

for %%a in (file1.txt) do set Fechh1=%%~ta
for %%a in (file2.txt) do set Fecha2=%%~ta

Then I want to compare the two dates and if the file2 date is newer than the file1 make a task (copy certain files).

But it's making the comparison as strings not as dates.

Upvotes: 0

Views: 614

Answers (1)

double-beep
double-beep

Reputation: 5504

The following code should work for you:

if exist "file1.txt" if exist "file2.txt" (
    for /F "skip=1 delims=" %%A IN ('dir /B /A-D /OD "file1.txt" "file2.txt"') do (
        if "%%A" == "file2.txt" (copy certain files) else (echo Unfortunately, file1.txt is newer)
    )
)

First, a check is performed in order to see if files file1.txt and file2.txt.

Then, check if the newest file is file2.txt via a dir command parsed in a for /F loop.

For more info about the commands used, please type the following commands in cmd:

  • if /?
  • for /?
  • copy /?
  • echo /?

Upvotes: 1

Related Questions