forfiles
is a computer software utility that runs a command for each file system item (file or directory) that matches selection criteria. The command was originally provided as an add-on, in the Windows 98, [1] Windows NT [ citation needed ] and Windows 2000 [2] Resource Kits. It became a standard utility with Windows Vista, as part of the new management features.
With no arguments, the command prints the name of every item in the working directory. The command supports the following command-line switches.
*.*
, this command treats *.*
and *
differently. The former only matches an item with a dot in its name, while the latter matches even with no dot or extension.CMD /C ECHO @FILE
, which prints, via ECHO
, the name of each matching item specified as variable @FILE
. See below for supported variables.0x22
or \"
.The following variables are replaced before the command is executed with metadata about the matched item.
.\
).TRUE
if the matching item is a directory, or FALSE
if not.The following command selects log files in the Windows directory 30 days or older, and lists them with their date. Since the command for option /C
must be a program, spawning a sub-shell (cmd.exe) via CMD /C
is required for shell builtin command ECHO
.
C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C ECHO @FDATE @FILE"6/12/2015 "iis7.log"5/28/2015 "msxml4-KB954430-enu.LOG"5/28/2015 "msxml4-KB973688-enu.LOG"5/26/2015 "setuperr.log"
The following command deletes the same files.
C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C DEL @PATH"
forfiles
find