Search code examples
windowspowershellbatch-filecmdforfiles

FORFILES date -after- (date calc in cmd file)


I'd like to use FORFILES in a cmd file, to act on all files after a given date thru "today". I can use something like forfiles /d +07/10/2013 /c "cmd /c echo @fname" to act on everything after 7/10/13, but what I want is to just be able to calculate instead from 90 days before "today".

Is there a syntax for the date calculation that will work in a cmd file that will let me specify "x days before today" to feed into FORFILES?

I prefer to not use VBS (and found a code snippet that would work in VBS), though I could alternatively re-write my script for Powershell, but ideally I want to stick with cmd.

To clarify, "-90" would find all files older than 90 days; "+90" would find all files newer than 90 days -after- today (which is fundamentally useless, as files are rarely written with future dates), and "+7/30/2013" will find all files newer than 7/30/2013. I want that last time period, preferably able to take a number-of-days variable passed to the CMD file, that would say "after x number of days before today", i.e. "in the last x days". So instead of using the hard-coded date as shown above, I want to be able to calculate that date within the cmd file.


Solution

  • In PowerShell you could do something like this:

    $refdate = (Get-Date).AddDays(-90)
    Get-ChildItem | Where-Object {
        $_.LastWriteTime -ge $refdate
    } | Select-Object -Expand Name