Forfiles
forfiles is a computer software utility that runs a command for each file system item that matches selection criteria. The 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.Use
With no arguments, the command prints the name of every item in the working directory. The command supports the following command-line switches.; /P path: The directory in which to search instead of the working directory. UNC paths are not accepted.
; /M pattern: Specifies a wildcard matching pattern that selects items for inclusion instead of all items. It results in filtering out items that do not match the pattern. It is matched on the item name but not the containing directory path. Without a wildcard, the pattern must exactly match the base name.
; /S: Match items in subdirectories in addition to the specified directory.
; /C command: Command to execute for each matching item. The command string typically must be wrapped in double quotes. The default command is, which prints, via, the name of each matching item specified as variable. See below for supported variables.
; /D date: Selects based on last modified date. By default, items are selected regardless of modified date.
; /?: Displays help information for the command.
Command variables
The following variables are replaced before the command is executed with metadata about the matched item.; @file: The name of the item, double quoted.
; @fname: The name of the item without extension, double quoted.
; @ext: The name extension, double quoted, without leading dot. If an item has multiple extensions, this evaluates to the last one. If the items has no extension, this evaluates to an empty string.
; @path: Full path of the matching item, double quoted, including drive letter, and extension.
; @relpath: Path of the matching item, double quoted, and relative to the starting directory. Each path begins with a dot and backslash.
; @isdir: Evaluates to the literal string if the matching item is a directory, or if not.
; @fsize: Size of the matching item, in bytes. Directories report a size of zero.
; @fdate: Date the item was last modified, in the localized date format of the current user.
; @ftime: Time the item was last modified, in the localized time format of the current user.
Examples
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 must be a program, spawning a sub-shell viaCMD/C is required for shell builtin command [echo (command)|].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"