0

If I want to list files that are at least one year old I use:

forfiles /S /M *.* /D -365 /C "cmd /c echo @file is at least one year old."

If I want to list files that have been created/changed today I use:

forfiles /S /M *.* /D +%DATE% /C "cmd /c echo @file has been changed today."

What would be the forfiles syntax to look dynamically for files that has been changed within the e.g last 48 hours?

For clarification: /D -2in my experience, lists me files that are at least 2 days old. I would like to have files that are not older than 2 days from the time the script was executed. Can this be done with forfiles. Inaccuracies (hours vs. days) would not bother me.

Peter S
  • 625
  • 1
  • 9
  • 32
  • 3
    `/D` works on `dd` and not hour, so if you add 2 days, if the file is not yet exactly 48 hours old, it will still be listed.. Quite frankly though I do not like `forfiles` at all. There are ways to use a for loop in batch, but I would recommend `powershell` for this. as it has some built in functionality to do it for you. `powershell "Get-ChildItem -recurse | where {$_.Lastwritetime -lt (Get-date).addhours(-48)}"` This one uses hours, but the same applies for `addminutes` – Gerhard Nov 24 '20 at 13:10
  • OK thanks but the command for 2days and not 48 hours would look like? – Peter S Nov 24 '20 at 13:30
  • 2
    `/D -2` but it will list files that were created 2 days ago, even though they are not exactly 48 hours old. In other words, a file created at `23:59` on `23 November` will be seen as 2 days old at `00:01` on `25 November` because `25 - 23 = 2 days` – Gerhard Nov 24 '20 at 13:50
  • Thanks I have extended my question to specify my desired solution. – Peter S Nov 24 '20 at 14:07
  • 1
    You would have to do the date math to subtract two days from the current date and then use that date with `FORFILES`. eg: `forfiles /D 11/22/2020` But I agree with Gerhard that calling out to Powershell is a better solution. – Squashman Nov 24 '20 at 14:44

0 Answers0