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.
Here is a work-around for the design flaw of
forfiles
concerning/D +dd
days, supposing that no items can be modified in the future, and which is based on two nestedforfiles
loops and relies on the fact thatforfiles
sets theErrorLevel
in case no items match the provided search criteria:The outer
forfiles
loop iterates through items that are at least as old as given by variableMINAGE
. The innerforfiles
loop, which iterates once at most as it recieves the iterated file@file
from the outer loop, returns the same file if it is also at least as old asMAXINC
(equalsMAXAGE
plus1
); if it is not,forfiles
sets theErrorLevel
to1
, which in turn is captured by the||
operator that executes the following command, namelyecho
, only in caseErrorLevel
has been set to a non-zero value.Both
MINAGE
andMAXAGE
must not be negative numbers (the commandsset "MAXAGE=%MAXAGE:*-=%"
andset "MINAGE=%MINAGE:*-=%"
remove the minus sign in case). The interim variableMAXINC
has been introduced in order to include the age specified byMAXAGE
itself into the returned result.The redirection
> nul
prevents the outerforfiles
from returning empty lines and the inner one from returning items that fulfil its search criteria (because we are interested in those that do not).2> nul
prevents the innerforfiles
loop from returning error messages in case its search criteria are violated.> con
overrides> nul
for theecho
command to actually return the required items.The
if @isdir==FALSE
part filters out directories so that only files are processed. ChangeFALSE
toTRUE
if you want to process only directories; remove it completely if you want to process both.In PowerShell you could do something like this: