10

I'm required to write a batch file to do a few things

Initially I thought my problem was very simple - capture the modified date of a txt file located in a specified directory, compare that date to the current date and if they are the same do something. If they are not then do something else.

The line I use to capture the current date is:

%date%

The lines I use to capture the modified date of my specified file is:

SET filename="C:\New Folder\New.txt"
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
ECHO %filedatetime:~0,-6% >> %destination%

In the above case I'm simply using echo to see what is returned and it seems as if the date is returned but I get extra information:

2012/02/19 02

I would like to know how to get the above values where they are comparable as well as how to compare them properly.

default
  • 11,485
  • 9
  • 66
  • 102
Kalim
  • 475
  • 1
  • 4
  • 10

4 Answers4

15

Working with dates is much harder in batch then it ought to be.

There is one command that can make your job easy in this case. FORFILES has the ability to process files that have been modified since a particular date. Use FORFILES /? from the command line to get documentation on its use.

This simple command will list all files that have been modified today:

forfiles /m * /d 0

If at least one file is found, then ERRORLEVEL is set to 0, else ERRORLEVEL is set to 1.

You have a specific file, so you can use

forfiles /m %filename% /d 0
if %errorlevel% == 0 (
  echo The file was modified today
  REM do whatever else you need to do
) else (
  echo The file has not been modified today
  REM do whatever else you need to do
)

There is a more concise way to do the above. The && operator is used to conditionally execute commands if the prior command was successful, || is used to conditionally execute commands if the prior command failed. However, be careful, the || commands will also execute if the && command(s) failed.

forfiles /m %filename% /d 0 && (
  echo The file was modified today
  REM do whatever else you need to do
) || (
  echo The file has not been modified today
  REM do whatever else you need to do
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This works well, but is there any way to silence the output it generates? – pcnate May 18 '15 at 15:09
  • @pcnate - Sure, simply redirect stdin and stderr to NUL. `forfiles /m %filename% /d 0 >nul 2>nul ...` – dbenham May 18 '15 at 15:32
  • @dbenharm Thx. It looks like this only works if the `/d - nn` matches the date modified. Any possible way to match anything newer than `/d - nn`? – pcnate Jun 03 '15 at 12:08
  • I opened a new question for this http://stackoverflow.com/questions/30620330/check-if-file-was-modified-after-xx-days-in-the-past – pcnate Jun 03 '15 at 12:38
10

I like dbenham's way, but if you want to make your code work you can do like this:

set currentDate=%date%
SET filename="C:\MyFile.txt"

FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
IF %filedatetime:~0, 10% == %currentDate% goto same

goto notsame

:same
echo Dates the same, do some code here

goto next

:notsame
echo Dates NOT the same, do some code here
goto end

:next

Thought it would be worth knowing how to make yours work just in case you need it again.

Community
  • 1
  • 1
SmithMart
  • 2,731
  • 18
  • 35
  • 5
    That may not always work - it doesn't on my machine because %date% is prefixed with the day of the week. Date formats change depending on the locale, one of the things that makes dates difficult. It's usually easy to make something work on a given machine, but a general solution is often difficult. – dbenham Feb 19 '12 at 21:24
  • True enough. I preferred your answer. Good to know about the day of the week thing, very random. – SmithMart Feb 19 '12 at 21:36
  • This works like a charm. You just saved me a** from being kicked :) – akhil May 12 '16 at 18:42
  • added fix for date prefix. SET d=%date% SET Day=%d:~7,2% SET Month=%d:~4,2% SET Year=%d:~10,4% REM Change / to - or as your locale setting. SET currentDate=%Month%/%Day%/%Year% – Zunair Nov 08 '16 at 21:26
2

Apart from the issues raised in the comments appended to the TechNet article that documents ForFiles ForFiles Documentation on Microsoft TechNet, there is another issue that is mentioned, but only if you read between the lines regarding time zone being ignored. Since ForFiles evaluates the Modified date reported in Local time, it will treat a file modified at 02:01 EST as older than a file modified at 02:59 EDT.

David A. Gray
  • 1,039
  • 12
  • 19
0

dbenham way worked but I noticed forfiles errorlevel is supposed to be 1 for errors so revised to be this:

forfiles /p C:\Users\[path] /m "special_file.txt" /d 0
        
IF ERRORLEVEL 1 (echo boo unsucessful and old file) else (echo yay sucessful and updated today)

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles https://ss64.com/nt/errorlevel.html

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 12:00