As @JosefZ already pointed out in his comment, if
is not capable of comparing date values.
However, to filter files by a certain range for the modification date, you could use two nested forfiles
loops:
> nul 2>&1 forfiles /D +01-12-2015 /C "forfiles forfiles /M @file /D -31-12-2105 /C 0x22cmd /C if 00x7840isdir==FALSE > con echo 00x7840fdate 00x7840file0x22"
Development
The outer loop of the two nested forfiles
loops enumerates items with the given lower bound of the range for the modification dates (the date is given in format dd-MM-yyyy
as per the (short) date format of my system; check out the help text of forfiles /?
to find out the proper format for your system):
forfiles /D +01-12-2015
This returns all items modified on 1st of December 2015 or later.
The inner loop is then used to return only items that have been modified on 31st of December 2015 or earlier. To achieve this, we need to create a command line based on the following:
forfiles /M * /D -31-12-2015 /C "cmd /C echo @fdate @file"
The mask after /M
needs to be set to the @file
value returned by the outer loop, so the inner one iterates once only per each iteration of the outer one; so the inner loop simply filters each single item received by the outer one by its modification date. The location after /P
does not need to be specified as the outer loop executes the inner loop (or in general the command after /C
) already from the directory the currently iterated item is located (this is also true if the /S
switch is provided to process sub-directories also).
The challenge now is to hide the variables like @file
of the inner loop from the outer loop. The trick is the use the hexadecimal code substitution feature of forfiles
; for instance, 0x20
will be replaced by a space, 0x22
by a quotation mark, 0x40
by the @
-sign and 0x78
is the letter x
. Since this substitution is done even before replacement of the @
-variables, we need to state 00x7840
to hide the @
symbol from the outer loop and thus to avoid replacement of @file
, for example, because the @
will become 0x40
, so the inner loop receives 0x40file
, which will be converted back to @file
and then immediately replaced by its value.
So the command line with the nested forfiles
loops could look like this:
forfiles /D +01-12-2015 /C "forfiles forfiles /M @file /D -31-12-2105 /C 0x22cmd /C echo 00x7840fdate 00x7840file0x22"
To filter out directories and return files only, you need to check @isdir
:
forfiles /D +01-12-2015 /C "forfiles forfiles /M @file /D -31-12-2105 /C 0x22cmd /C if 00x7840isdir==FALSE echo 00x7840fdate 00x7840file0x22"
Finally, to avoid empty lines or error messages to be returned by forfiles
, use redirection:
> nul 2>&1 forfiles /D +01-12-2015 /C "forfiles forfiles /M @file /D -31-12-2105 /C 0x22cmd /C if 00x7840isdir==FALSE > con echo 00x7840fdate 00x7840file0x22"
Consult also the following posts: