This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Selects and runs a command on a file or set of files. This command is most commonly used in batch files.
forfiles [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] [{<date> | <days>}]]| Parameter | Description |
|---|---|
/P<pathname> | Specifies the path from which to start the search. By default, searching starts in the current working directory. |
/M<searchmask> | Searches files according to the specified search mask. The default searchmask is*. |
| /S | Instructs theforfiles command to search in subdirectories recursively. |
/C<command> | Runs the specified command on each file. Command strings should be wrapped in double quotes. The default command is"cmd /c echo @file". |
/D[{+\|-}][{<date> | <days>}] | Selects files with a last modified date within the specified time frame:
|
| /? | Displays the help text in the cmd window. |
Theforfiles /S command is similar todir /S.
You can use the following variables in the command string as specified by the/C command-line option:
| Variable | Description |
|---|---|
| @FILE | File name. |
| @FNAME | File name without extension. |
| @EXT | File name extension. |
| @PATH | Full path of the file. |
| @RELPATH | Relative path of the file. |
| @ISDIR | Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE. |
| @FSIZE | File size, in bytes. |
| @FDATE | Last modified date stamp on the file. |
| @FTIME | Last modified time stamp on the file. |
Theforfiles command lets you run a command on or pass arguments to multiple files. For example, you could run thetype command on all files in a tree with the .txt file name extension. Or you could execute every batch file (*.bat) on drive C, with the file name Myinput.txt as the first argument.
This command can:
Select files by an absolute date or a relative date by using the/d parameter.
Build an archive tree of files by using variables such as@FSIZE and@FDATE.
Differentiate files from directories by using the@ISDIR variable.
Include special characters in the command line by using the hexadecimal code for the character, in 0xHH format (for example, 0x09 for a tab).
This command works by implementing therecurse subdirectories flag on tools that are designed to process only a single file.
To list all of 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 of 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 of the files in the current directory that are at least one year old, type:
forfiles /S /M *.* /D -365 /C "cmd /c echo @file is at least one year old."To display the textfile is outdated for each of the files in the current directory that are older than January 1, 2007, type:
forfiles /S /M *.* /D -01/01/2007 /C "cmd /c echo @file is outdated."To list the file name extensions of all the files in the current directory in column format, and add a tab before the extension, type:
forfiles /S /M *.* /C "cmd /c echo The extension of @file is 0x09@ext"Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?