Updated: April 25, 2007
Selects and executes a command on a file or set of files. This command is useful for batch processing.
For examples of how to use this command, see Examples.
forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c "<Command>"] [/d [{+|-}][{<Date>|<Days>}]]
/p <Path>
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 search mask is *.*.
/s
Instructs the forfiles command to search into subdirectories recursively.
/c "<Command>"
Runs the specified command on each file. Command strings should be enclosed in quotation marks. The default command is "cmd /c echo @file".
/d [{+|-}][{<Date>|<Days>}]
Selects files with a last modified date within the specified time frame.
/?
Displays help at the command prompt.
@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.
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 text "File 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"
Command-Line Syntax Key
does not work because the /m switch means the command will only display files. Also, the boolean values TRUE and FALSE used in the variables are CASE SENSITIVE as suggested but not outright stated in the Remarks section. Removing the /m switch and using true in ALL CAPS like this:
forfiles /p c:\ /s /c "cmd /c if @isdir==TRUE echo @file is a directory"lets the command complete as desired
lets the command complete as desired
[tfl - 12 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.frameworkPowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&