Forfiles

Last updated
forfiles
Developer Microsoft
Operating system Microsoft Windows
Type Command
License Proprietary commercial software
Website docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles

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.

Contents

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 (\\machine\share) 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 (including extension) but not the containing directory path. Without a wildcard, the pattern must exactly match the base name.
Even though most DOS/Windows commands match files with no extension for the pattern *.*, 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.
/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 CMD /C ECHO @FILE, which prints, via ECHO, the name of each matching item specified as variable @FILE. See below for supported variables.
A hexadecimal value with a "0x" prefix is treated as an ASCII character code which allows for the use of non-printing or extended ASCII characters. A double quote can be represented as either 0x22 or \".
/D date
Selects based on last modified date. By default, items are selected regardless of modified date.
A date argument is formatted as MM/DD/YYYY or as a number; an age in days (i.e., the day date days before the present date). If the argument begins with a minus (-), only items modified on or before the date are selected (older item / modified earlier). Otherwise, only items modified on or after the given date are selected (younger items / modified later). An explicit plus (+) may be given, but is the default. Note that both modes select items on the given date. There is no way to select items only on a given date (without also either before or after).
/?
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 (if any).
@relpath
Path of the matching item, double quoted, and relative to the starting directory (given by /P). Each path begins with a dot and backslash (.\).
@isdir
Evaluates to the literal string TRUE if the matching item is a directory, or FALSE 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 /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"

See also

References

  1. "Forfiles.exe - Windows 98 Resource Kit Utility". Archived from the original on 2020-01-11. Retrieved 2020-01-11.
  2. "Forfiles.exe - Windows 2000 Resource Kit Utility". Archived from the original on 2020-01-11. Retrieved 2020-01-11.