{{lowercase}} {{Infobox Software | name = forfiles | logo = | screenshot = | screenshot size = | caption = | developer = Microsoft | released = | latest release version = | latest release date = | operating system = Microsoft Windows | genre = Command | license = Proprietary commercial software | website = {{URL|https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles}} }} '''<code>forfiles</code>''' 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,<ref>{{Cite web |url=https://www.activexperts.com/admin/reskit/reskit98/forfiles/ |title=Forfiles.exe - Windows 98 Resource Kit Utility |access-date=2020-01-11 |archive-date=2020-01-11 |archive-url=https://web.archive.org/web/20200111203651/https://www.activexperts.com/admin/reskit/reskit98/forfiles/ |url-status=dead }}</ref> Windows NT{{cn|date=January 2020}} and Windows 2000<ref>{{Cite web |url=https://www.activexperts.com/admin/reskit/reskit2000/forfiles/ |title=Forfiles.exe - Windows 2000 Resource Kit Utility |access-date=2020-01-11 |archive-date=2020-01-11 |archive-url=https://web.archive.org/web/20200111203652/https://www.activexperts.com/admin/reskit/reskit2000/forfiles/ |url-status=dead }}</ref> 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 (\\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 {{code|*.*}}, this command treats {{code|*.*}} and {{code|*}} 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 {{code |CMD /C ECHO @FILE}}, which prints, via {{code |ECHO}}, the name of each matching item specified as variable {{code |@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 {{code|0x22}} or {{code|\"}}.
; /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 ({{code|.\}}).
; @isdir: Evaluates to the literal string {{code |TRUE}} if the matching item is a directory, or {{code |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 {{code|/C}} must be a program, spawning a sub-shell (cmd.exe) via <code>CMD{{nbsp}}/C</code> is required for shell builtin command {{code |ECHO}}.
<syntaxhighlight lang="doscon"> 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" </syntaxhighlight>
The following command deletes the same files.
<syntaxhighlight lang="doscon"> C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C DEL @PATH" </syntaxhighlight>
== See also == * {{Annotated link |cmd.exe}} * {{Annotated link |Foreach loop}} * {{Annotated link |find (Unix)}}; functionally similar to <code>forfiles</code> * {{Annotated link |find (Windows)}} * {{Annotated link |grep}}; functionally similar to Windows <code>find</code>
== References == {{Reflist}} <!--
Following are some general references. These sources were used to write the vast majority of the article content. The article has a narrow focus (a single small program) and thus almost the entire article is generally sourced.
It is inaccurate to convert these to inline citations attached to some single small part of the article. They were not used for just selected parts; they were used to create the entire article. While they could be attached to just about every sentence individually, this serves no useful purpose.
It is inaccurate to convert these to "Further reading". They are not *further* reading, but the source material for the reading already done (namely, reading the article).
I am aware of WP:OWN, but I object strongly to misattributing content that is sourced elsewhere without giving credit. It is inaccurate and verging on plagiarism to do otherwise.
See WP:CITE, especially WP:CITETYPE, for more.
If you think I am interpreting WP:CITE incorrectly, please engage me on the Talk page.
-- User:DragonHawk, 2025 September 27
--> * {{citation |title=FORFILES |work=Microsoft Windows |publisher=Microsoft |date=13 July 2009 |type=Computer software |version=6.1.7600.16385 }}. * {{cite web |title=Forfiles |publisher=Microsoft |date=April 17, 2012 |work=TechNet |url=https://technet.microsoft.com/en-us/library/cc753551.aspx |access-date=28 April 2016 }} * {{cite web |title=FORFILES.exe |work=SS64 |author=Simon Sheppard |url=http://ss64.com/nt/forfiles.html |access-date=28 April 2016 }}
==External links== {{Wikibooks|Guide to Windows Commands}} *[https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles forfiles | Microsoft Docs]
{{Windows commands}}
Category:Windows Vista Category:Utility software Category:Command-line software Category:Windows administration