Forfiles


forfiles is a computer software utility for Microsoft Windows, which selects files and runs a command on them. File selection criteria include name and last modified date. The command specifier supports some special syntax options. It can be used directly on the command-line, or in batch files or other scripts.
The forfiles command was originally provided as an add-on, in the Windows 98, Windows NT and Windows 2000 Resource Kits. It became a standard utility with Windows Vista, as part of the new management features.

Usage

The forfiles command has several command-line switches. If no switches or parameters given, it outputs the name of every file in the current directory.

Switches

Command syntax

The command string is executed as given, except as noted below.
Sequences of the form, where "0x" is literal, and "FF" represents any two-digit hexadecimal number, are replaced with the corresponding single-byte value. This can be used to embed non-printing ASCII characters, or extended ASCII characters.
The sequence is replaced with a literal quotation mark .
Several variables are provided, to be used in the command as placeholders for the values from each file. Variables are technically not required, but must be used if the command is to vary for each file.
VariableMeaning
@fileThe name of the matching item, double quoted.
@fnameThe basename of the matching item, double quoted.
@extThe file extension, double quoted, without leading dot. If a file has multiple extensions, only the last is returned. If the file has no extension, a quoted empty string is returned.
@pathFull path of the matching item, double quoted, including drive letter, and file extension.
@relpathPath of the matching item, double quoted, and relative to the starting directory. Each path begins with a dot and backslash.
@isdirEvaluates to the literal string if the matching item is a directory, or if not.
@fsizeSize of the matching item, in bytes. Directories report a size of zero.
@fdateDate the file was last modified, in the localized date format of the current user.
@ftimeTime the file was last modified, in the localized time format of the current user.

Date syntax

The date switch selects files based on their last modified date, given a date argument.
The date argument can be given as a literal date, in MM/DD/YYYY format. Alternatively, the date argument can be given as a number, in which case it is taken to mean an age in days.
If the date argument begins with a minus, only files modified on or before the given date are selected. Otherwise, only files modified on or after the given date are selected. An explicit plus may be given, but is the default. Note that both modes select files on the given date. There is no way to select files only on a given date.

Examples

The following command selects all log files in the Windows directory 30 days or older, and lists them with their date.

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 would delete the same files.

C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C DEL @PATH"

The use of is required in the above examples, as both echo | and del | are internal to the command processor, rather than external utility programs.