1

The command I am executing is forfiles -p"C:\testdata" -m*.* -d-1 -c"cmd /c echo @PATH\@FILE"

I have specified that I wish to only list files that are a day old however when I execute the statement, it returns me a list of files that were created today.

Why is that? Am I doing something wrong? Would it be better to specify a time period as opposed to a date e.g. 24 hours?

The version of forfiles I have reads as follows FORFILES v 1.1 - emmanubo@microsoft.com - 4/98. The batch file is being run on Windows XP.

4 Answers4

1

From the fine documentation (emphasis mine):

Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.

Edit:

Perhaps you could try with a VBScript:

Set args = WScript.Arguments.Named
Set fso  = CreateObject("Scripting.FileSystemObject")

d = 0
p = "."

If args.Exists("?") Or args.Count = 0 Then
  WScript.Echo "Usage: cscript " & WScript.ScriptName & " [/d:DAYS] [/p:PATH]"
  WScript.Quit(0)
End If

If args.Exists("d") Then d = args("d")
If args.Exists("p") Then p = args("p")

refDate = DateAdd("d", d, Now)
TraverseFolders fso.GetFolder(p)

Sub TraverseFolders(fldr)
  For Each f In fldr.Subfolders
    TraverseFolders(f)
  Next
  For Each f In fldr.Files
    If f.DateLastModified < refDate Then
      WScript.StdOut.WriteLine f.Path
      'f.Delete
    End If
  Next
End Sub
1

"... I wish to list only files that are a day old ..."

Do you mean "files that were created yesterday"?  I.e., (today being September 18), files whose modification date/time is ≥ 0000 (midnight in the early morning of) Sept. 17, and ≤ 2359.60 (midnight at the end of) Sept. 17.  Well, that's two date/time constraints, and forfiles doesn't let you specify more than one.  Bottom line: I believe that forfiles will not do that.

...  However, when I try /d -1 on my Windows 7 box, I get files that were created yesterday or before.  So maybe the Windows XP version of forfiles is broken.

1

Forfiles version you use is not native XP command. It's an older version of the utility, which was originally distributed with one of the W2K Resource Kits (I think it goes even to NT Resource kit, although I'm not sure). There is nothing wrong using that on XP per se , but if you look closely into it's syntax, it says (emphasis mine):

-d[+|-][DDMMYY|DD] Select files with date >= or <=DDMMYY (UTC) or files having date >= or <= (current date - DD days)

Note UTC in the description. This means (1.1) forfiles uses different time stamps that you think it does - unless your system time is UTC (no offset), no daylight savings.
This will be probably easier with an example. I'm UTC+1, daylight savings time.
(today's date - 2012-09-19)

  • Create file and do a dir:

2012-09-19 00:14 5 zzz_utc.txt

it has today's date, so it should not be selected by forfiles,let's check it:

FORFILES -pc:\temp -mzzz_utc.* -d-1 -c"CMD /C Echo @FILE"
zzz_utc.txt

But it does get selected! Why? Let's look at file UTC times (following command is run from powershell):

ls .\zzz_utc.txt |select LastAccessTime,LastAccessTimeUTC

LastAccessTime                          LastAccessTimeUtc
--------------                          -----------------
2012-09-19 00:15:11                     2012-09-18 22:15:11

As you can see, TimeUtc - the one used by (1.1) forfiles is from yesterday! That's why it gets selected by the tool.

wmz
  • 7,358
0

Seems FORFILES /D -1 has a bug even on Windows 8 and Windows Server 2008 R2 (can't make it to work - selects all files - on a system with Greek dates)

You could parse the %date% as suggested in some comment above, but note that on Windows 8 for example the date is showing 3 letters with day name and a space char first, then the classic date format. Also that date will be in localized format, whereas FORFILES /D expects a date in dd/MM/yyyy format according to forfiles /? (at least on Win8).

CORRECTION: there seems to be a misunderstanding (by me and others too judging from a search on the net) about what /D -dd does, seems it doesn't search for files being dd days old, but being older than dd days

so you need to use the /D +dd/MM/yyyy syntax of FORFILES and pass in yesterday's date there to find all files with date greater than yesterday. To automate this you could use %date% and parse it with %date:~7,2%/%date:~4,2%/%date:~-4% or something like that (may need to reorder the date parts there depending on your locale)