Click to Rate and Give Feedback
TechNet
TechNet Library
Windows Server
A-Z List
 Forfiles
Forfiles

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.

Syntax

forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c "<Command>"] [/d [{+|-}][{<Date>|<Days>}]]

Parameters

 

Parameter Description

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

  • Selects files with a last modified date later than or equal to (+) or earlier than or equal to (-) the specified date, where Date is in the format MM/DD/YYYY.
  • Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.
  • Valid values for Days include any number in the range 0–32,768. If no sign is specified, + is used by default.

/?

Displays help at the command prompt.

Remarks

  • Forfiles is most commonly used in batch files.
  • Forfiles /s is similar to dir /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.

  • With forfiles, you can run a command on or pass arguments to multiple files. For example, you could run the type 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.
  • With forfiles, you can do any of the following:
    • 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 @FSIZEand @FDATE.
    • Differentiate files from directories by using the @ISDIRvariable.
    • Include special characters in the command line by using the hexadecimal code for the character, in 0xHH format (for example, 0x09 for a tab).
  • Forfiles works by implementing the recurse subdirectories flag on tools that are designed to process only a single file.

Examples

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" 

Additional references

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
PARAMETERS and CASE SENSITIVITY      mfinky   |   Edit   |   Show History
1. parameters. not sure why, but on my PC the correct syntax seems to be
Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-dDDMMYY] [-s]

Dashes, not slashes. and no spaces between the parameter name and the value.

2. Variables must be typed using ALL CAPS. Not as seen on the examples on this page. e.g.: @FILE, instead of "@file".

HTH,
Marcelo Finkielsztein
How do I delete FOLDER with FORFILES or any other method for the folder older then x days?      rawcane ... David Iraheta   |   Edit   |   Show History
It Works for me.
forfiles /p "%programfiles%\logs" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /D -30

David Iraheta
El Salvador
/D switch      ucramos   |   Edit   |   Show History
There is something very wrong with the D switch

For example... Imagine that today is 15 of July of 2011
If i put -10 on the switch i will get the files OLDER than 05 of July of 2011
If i put +10 on the switch i will get the files NEWER than 25 of July of 2011.
Ok... Why would i want a to use files from the future? :)
This switch should use the +10 to give me the files NEWER than 05 of July of 2011.

Am i seing this the wrong way? I´ve tested it by changing the date on the computer to test the + 10 and those are the results i get. Please Help !

I want to copy NEWER files than TODAY minus X days to another dir and i can´t manage to get it done
if i use the -10 it gets me the OLDER FILES than 10 days ago... if i use the +10 it says that it can´t find any files... and i have a lot of files from 10 days ago
Tags What's this?: Add a tag
Flag as ContentBug
/d option      itchniba ... HBunny   |   Edit   |   Show History
$0 When using the &;;lt;Days&;;gt; in the /d option, a positive number has the following behaviour: $0 $0 $0 $0 "Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified" $0 $0 $0 $0 This means that it'll select files that were modified in the future? How is this useful? $0


That can be useful e.g. to list files that were imported from a different Operating System with an incompatible date format. Sometimes such files have weird Created/Modified dates.
Example of How to Delete Folders      Louis Nguyen   |   Edit   |   Show History

This example, removes directories older than 365 days in c:\temp.

forfiles /p "c:\temp" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /d -365
Tags What's this?: Add a tag
Flag as ContentBug
/m option and folders      CitizenRon ... Peter Wawa   |   Edit   |   Show History
The example shown above:


forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"

does not work because the /m switch means the command will only display directories, which name contains "." . 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

Tags What's this?: Add a tag
Flag as ContentBug
@FSIZE always returns 0 for folders      Drewfus   |   Edit   |   Show History
@FSIZE will always return 0 (Bytes) for a folder, regardless of actual size.

Is there a way of making forfiles @FSIZE return the actual size of folders?
Tags What's this?: Add a tag
Flag as ContentBug
Confirmation prompt      Neurosine ... Thomas Lee   |   Edit   |   Show History
Is there a way to suppress the confirmation prompt?

[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.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&;;

Time Zone      Dutchie   |   Edit   |   Show History
Please keep in mind that the time that is returned for a file does not consider your local time zone offset.
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker