Forfiles - DOS/Command Prompt Reference
[Windows Vista or later] Executes commands for each file/subdirectory in the directory.
Syntax
forfiles[.exe] [/P <path>] [/S] [/M <mask>] [/C <command>] [/D [+ | -][yyyy/MM/dd | dd]]
Options
/P <path> | Specifies the parent directory for the file or subdirectory on which the command is to be executed. The default is ‘.’ (current directory). |
/S | Expands the scope of searching (enumerating) files and subdirectories to include subdirectories. By default, the search is limited to the immediate contents of the target directory. |
/M <mask> | Specifies the conditions for filtering names of files and subdirectories to narrow down the search. Wildcards can be used, including the extension part, for the <mask> parameter. The default is ‘*’ which enumerates all files/subdirectories. |
/C <command> |
Specifies the command line to be executed. If the command line contains spaces, it must be enclosed in double quotation marks ‘" "’. If omitted, it is assumed to be ‘ In the command line, you can use special notations to obtain file names, etc. For more details, please refer to the Details section. |
/D [+ | -][yyyy/MM/dd | dd] |
Specifies the last modification date (in days) for filtering (filter) files and subdirectories. The format will be ‘+’ or ‘-’ followed by the number of days without spaces to indicate the date or ‘N days later (before)’. The ‘+’ or ‘-’ is optional, and if omitted, it is treated as ‘+’. ‘+’ or ‘-’ means whether to filter for dates ‘on or after’ or ‘on or before’. ‘+’ filters for ‘on or after a specific date’, while ‘-’ filters for ‘on or before a specific date’. Note that the ‘time’ is ignored, and the comparison is based solely on the ‘date’ part. ‘yyyy/MM/dd’ or ‘dd’ represents the actual filtering range. For ‘yyyy/MM/dd’ format, specify the starting date in the format ‘year (4 digits)’/‘month (2 digits)’/‘day (2 digits)’. On the other hand, specifying only ‘dd’ represents the number of days from the current date. For example, ‘+5’ indicates 5 days later, ‘-3’ indicates 3 days earlier, and the respective dates are used as the starting point for the filtering range, including dates on or after/in or before those specific dates. |
Details
Forfiles is used when you want to enumerate files or subdirectories and execute a specific command, similar to the For command. While For is an internal command of cmd.exe, Forfiles is an independent program, so the usage in batch files may differ. Please note that when using internal commands (such as the Echo command or Copy command), you need to explicitly specify the use of the Cmd program as in ‘forfiles /C "cmd /S /C \"command @file\""’.
In the command line specified for Forfiles, you can use the following special identifiers to obtain information about the enumerated files and subdirectories. These identifiers allow you to gather information about the files and subdirectories being enumerated.
Identifier | Meaning | Expansion sample |
---|---|---|
@file | The file name. | "note.201311.txt" |
@fname | The file name without extensions. | "note.201311" |
@ext | The file extension without dot. | "txt" |
@path | The full path name of the file. | "D:\MyDocs\Text Documents\note.201311.txt" |
@relpath | The relative path name of the file, with the path specified by /P as the base. | ".\Text Documents\note.201311.txt" |
@isdir | If the target is a directory, it will be ‘TRUE’; if it's a file, it will be ‘FALSE’. | FALSE |
@fsize | The file size in bytes. For directories, it is 0. (There are no separators or units specified.) | 34122 |
@fdate | The last modification date of the file. The format follows the system setting's ‘short date format’. | 11-30-2013 |
@ftime | The last modification time of the file. The format follows the system setting's ‘short time format’. | 17:25:23 |
Additionally, the following characters can be used in the command line.
0xHH
(2 digits in hexadecimal) - The value is used as the character code (including extended ASCII character codes), and the corresponding character is used. (It is treated as an ASCII character code regardless of the current environment's code page.)\"
- When using a double quotation mark " in the command line, prepend it with a backslash ‘\’ to make it ‘\"’. Especially when the program path specified at the beginning of the command line contains spaces, you may need to format it like\"X:\Path To\Program.exe\" parameters...
(However, there seems to be a bug(?) in the interpretation, and if you do this, the beginning of the command line passed to the program becomes a double quotation mark ").
* When Forfiles executes a command, it does not include the program name in the command line it passes to the program (similar to the elements of argv in C/C++ being shifted by one at the beginning). Therefore, some programs may not function properly unless a dummy argument is placed immediately after the program name.
Samples
Sample 1
forfiles /M *.*
Enumerate files/directories with existing extensions and output them to the screen (including files starting with ‘dot’). This specification means that it narrows down to files with names containing "dot" only, so files/directories without extensions, which do not contain ‘dot’, are excluded.
Sample 2
forfiles /P %CD%\data\2017 /S /M *.xml /C "\"D:\My Tools\Parser.exe\" Parser.exe\" /I @path /O \"%CD%\output\2017\""
Enumerate files with the extension "xml" in the ‘data\2017’ directory and its subdirectories under the current directory, and execute ‘D:\My Tools\Parser.exe’ with their absolute path names as part of the arguments. Here, a dummy argument is added to the command line, so the beginning of the Parser.exe command line is set as ‘"Parser.exe"’. For example, if ‘Y:\My Documents\data\2017\01.xml’ is enumerated, the command line passed to Parser.exe would be ‘"Parser.exe" /I "Y:\My Documents\data\2017\01.xml" /O "Y:\My Documents\output\2017"
’.
* This specified format is due to the interpretation of Forfiles program's arguments as mentioned earlier. Therefore, it is recommended to verify in advance what command line will be passed to the program if the program name is enclosed in ‘"’.