Forfiles
Select the file you want to batch from the folder or tree.
Grammar
forfiles [/P Path] [/M searchmask] [/s] [/C Command] [/d[{+ |-}] [{mm/dd/yyyy | DD}]]
Parameters
/P Path
Specify Path, indicating where to start the search. The default folder is the current working directory, which is specified by typing a period (.).
/M Searchmask
Search for files by Searchmask. The default searchmask is *.*.
/s
Instructs Forfiles to search in subdirectories.
/C Command
Runs the specified Command on each file. command strings with spaces must be enclosed in quotation marks. The default Command is "cmd/c echo @file".
/d[{+ |-}] [{mm/dd/yyyy | DD}]
Select a file with a date that is greater than or equal to (+) (or less than or equal to (-)) The specified date, where mm/dd/yyyy is the specified date and DD is the current date minus DD days. If + or-is not specified, the + is used. The valid range for DD is 0-32768.
/?
Displays help at the command prompt.
Comments
Forfiles is most commonly used in batch files.
The forfiles/s is similar to dir/s.
The following table lists the variables that can be used in the/ccommand command string.
variable |
Description |
@file |
Filename |
@fname |
File name with no extension |
@ext |
File name extension |
@path |
Full path to File |
@relpath |
Relative path of File |
@isdir |
If the file type is a directory, the evaluates to TRUE, otherwise the value is FALSE |
@fsize |
File size in bytes |
@fdate |
Last modified date stamp in file |
@ftime |
Last modified timestamp in file |
With Forfiles, you can run commands on multiple files or pass parameters to multiple files. For example, you can run the TYPE command on all files with *.txt extensions in the tree. Alternatively, you can use the filename "Myinput.txt" as the first argument, in the C:\ Execute each batch file (*.bat) on the drive.
By using Forfiles, you can perform any of the following actions:
Use/d to select files by absolute or relative date.
Build an archive tree of files using variables such as @fsize (file size) and @fdate (file date).
Use @isdir variables to differentiate between files and directories.
Format the output by including special characters on the command line and by using the hexadecimal code 0xHH wrapping character.
Forfiles works by executing the "loop subdirectory" tag on a tool designed to work with only a single file.
Example
To list all the batch files on drive C:, type:
forfiles/p c:\/s/m*.bat/c "cmd/c Echo @file is a batch file"
To list all the directories on drive C:, type:
forfiles/p c:\/s/m*.*/C "cmd/c if @isdir ==true echo @file is a directory"
To list all files that have more than 100 days on the drive C:, type:
forfiles/p c:\/s/m*.*/dt-100/c "cmd/c echo @file:d ate >= days"
To list all the files that were created prior to January 1, 1993 on drive C: and for files that have a date earlier than January 1, 1993 to display the file is quite old!, type:
forfiles/p c:\/s/m*.*/dt-01011993/c "cmd/c Echo @file is quite old!"
To list the extensions for all files on drive C: In column format, type:
forfiles/p c:\/s/m*.*/C "cmd/c echo extension of @file is 0x09@ext0x09" with:
To list all the batch files on drive C:, type:
forfiles/p c:\/s/m *.bat/c "cmd/c Echo @file is a batch file"
To list all the directories on drive C:, type:
forfiles/p C:\/s/m *.*/C "cmd/c if @isdir ==true echo @file is a directory"
To list all files that have more than 100 days on the drive C:, type:
forfiles/p c:\/s/m *.*/d t-100/c "cmd/c echo @file:d ate >= days"
To list all the files that were created prior to January 1, 1993 on drive C: and for files that have a date earlier than January 1, 1993 to display the file is quite old!, type:
forfiles/p c:\/s/m *.*/d t-01011993/c "cmd/c Echo @file is quite old!"
To list the extensions for all files on drive C: In column format, type:
forfiles/p c:\/s/m*.*/C "cmd/c echo extension of @file is 0x09@ext0x09"
format Legend
format |
meaning |
Italic Body |
Information that the user must provide |
Bold Body |
The user must accurately type the element shown |
Ellipsis (...) |
Parameters that can be repeated multiple times on the command line |
Between brackets ([]) |
Optional items |
Between braces ({}), separating the options with a vertical bar (|). Example: {even|odd} |
Option group from which the user must select only one option |
Courier font
|
Code or program output |