FORFILES date -after- (date calc in 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 nested forfiles loops and relies on the fact that forfiles sets the ErrorLevel in case no items match the provided search criteria:

rem Define minimum and maximum age in days here (0 means today):
set /A MINAGE=0, MAXAGE=90

set "MAXAGE=%MAXAGE:*-=%" & set "MINAGE=%MINAGE:*-=%" & set /A MAXINC=MAXAGE+1
> nul forfiles /D -%MINAGE% /C "cmd /C if @isdir==FALSE 2> nul forfiles /M @file /D -%MAXINC% || > con echo @fdate  @file"

The outer forfiles loop iterates through items that are at least as old as given by variable MINAGE. The inner forfiles 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 as MAXINC (equals MAXAGE plus 1); if it is not, forfiles sets the ErrorLevel to 1, which in turn is captured by the || operator that executes the following command, namely echo, only in case ErrorLevel has been set to a non-zero value.

Both MINAGE and MAXAGE must not be negative numbers (the commands set "MAXAGE=%MAXAGE:*-=%" and set "MINAGE=%MINAGE:*-=%" remove the minus sign in case). The interim variable MAXINC has been introduced in order to include the age specified by MAXAGE itself into the returned result.

The redirection > nul prevents the outer forfiles 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 inner forfiles loop from returning error messages in case its search criteria are violated. > con overrides > nul for the echo command to actually return the required items.

The if @isdir==FALSE part filters out directories so that only files are processed. Change FALSE to TRUE if you want to process only directories; remove it completely if you want to process both.

Leave a Comment