forfiles

{{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}}

}}

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,{{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 }} Windows NT{{cn|date=January 2020}} and Windows 2000[https://www.activexperts.com/admin/reskit/reskit2000/forfiles/ Forfiles.exe - Windows 2000 Resource Kit Utility] 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 =

class="wikitable sortable"

! Switch

! Argument

! Meaning

/P

| pathname

| The directory (folder) to search for files in. The default is the current directory. UNC paths (\\machine\share) are not accepted.

/M

| searchmask

| A glob pattern (wildcard search). Only files whose filename matches the pattern are selected. The file extension is included in the filename; the path (folder name) is not. The pattern must match the entire name, or use wildcards. The default is to match all files.

This option treats glob patterns {{code|*.*}} and {{code|*}} differently. The former will only match files with a dot in their name, while the latter will match even those with no dot or extension. Most DOS/Windows commands will match files with no extension even when given a {{code|*.*}} pattern.

/S

| (none)

| Selects matching files in subdirectories. By default, only the single, specified directory is searched.

/C

| command

| Execute the given command for each matching file. The command string typically needs to be wrapped in double quotes. See below for the syntax within the command string. The default command is {{code|CMD /C ECHO @FILE}}, which causes the name of each matching file to be output (displayed).

/D

| date

| Selects files based on their last modified date. See below for date syntax. By default, files are selected regardless of their date.

/?

| (none)

| Displays a help message (brief usage instructions). Suppresses file search/command execution. May not be used with any other switch.

= Command syntax =

The command string is executed as given, except as noted below.

Sequences of the form {{code|0xFF}}, 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 {{code|\"}} is replaced with a literal quotation mark {{code|"}}. Using the 0x sequence form described previously, {{code|0x22}} can also be used, which additionally hides the {{code|"}} from the command interpreter.

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.

class="wikitable sortable"

!Variable

!Meaning

@file

| The name of the matching item, double quoted.

@fname

| The basename of the matching item (without file extension), double quoted.

@ext

| The 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.

@path

| Full path of the matching item, double quoted, including drive letter, and file 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 file was last modified, in the localized date format of the current user.

@ftime

| Time the file was last modified, in the localized time format of the current user.

= Date syntax =

The date switch (/D) 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 (other date formats are not accepted). Alternatively, the date argument can be given as a number, in which case it is taken to mean an age in days (i.e., the day date days before the present date).

If the date argument begins with a minus (-), only files modified on or before the given date are selected (older file / modified earlier). Otherwise, only files modified on or after the given date are selected (younger files / modified later). 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 (without also either before or after).

Examples

The following command selects all log files (*.LOG) 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 {{code|CMD /C}} is required in the above examples, as both echo (command) and del (command) are internal to the command processor, rather than external utility programs.

See also

  • cmd.exe – The program implementing the Windows command-line interpreter
  • Foreach loop – The FOR and FORFILES commands both implement a for-each loop
  • find (Unix) – Unix command that finds files by attribute, similar to forfiles
  • find (Windows) – DOS and Windows command that finds text matching a pattern
  • grep – Unix command that finds text matching a pattern, similar to Windows find

Further reading

  • {{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

}}

References

{{Reflist}}