The Forfiles command on Windows selects and executes a command on a file or set of files. Forfiles is ideal for batch processing through scripts. For instance on Windows Server systems. 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 extension. Or you could execute every batch file (*.bat) on drive C, with the file name “Myinput.txt” as the first argument.

The Forfiles command syntax is:

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

Forfiles on Windows works by implementing the recurse subdirectories flag on tools that are designed to process only a single file. Lets take an example I use a lot as a scheduled task, and break it down.

Using forfiles, you can easily batch delete files older than 7 days in Windows.

Forfiles batch process and delete files older than ‘n’ days

An example to batch delete all files older than seven days in C:\Windows\Temp:

ForFiles /S /P C:\Windows\Temp /D -7
   /C "cmd /c if @isdir==FALSE del /F /Q @file"
  • /S : Instructs forfiles to recurse into subdirectories. Like “DIR /S”
  • /P [pathname] : Indicates the path to start searching. The default folder is the current working directory (.).
  • /D [date] : Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the “MM/dd/yyyy” format; or selects files with a last modified date greater than or equal to (+) the current date plus “dd” days, or less than or equal to (-) the current date minus “dd” days. A valid “dd” number of days can be any number in the range of 0 – 32768.
    “+” is taken as default sign if not specified.
  • /C [command] : Indicates the command to execute for each file.
    Command strings should be wrapped in double quotes. The default command is “cmd /c echo @file”. The following variables can be used in the command string:
    @file – returns the name of the file.
    @fname – returns the file name without extension.
    @ext – returns only the extension of the file.
    @path – returns the full path of the file.
    @relpath – returns the relative path of the file.
    @isdir – returns “TRUE” if a file type is a directory, and “FALSE” for files.
    @fsize – returns the size of the file in bytes.
    @fdate – returns the last modified date of the file.
    @ftime – returns the last modified time of the file.
Read this too:   HTTP to HTTPS redirect using IIS web.config

So what this example does, is delete all files older than one (1) day, in C:\Windows\Temp using Forfiles on Windows. This Forfiles command is ideal for batch processing, to delete files older than ‘n’ days using a .bat file.

7 replies
  1. Abhinav
    Abhinav says:

    Can you please help me how can i use forfiles to delete all *.txt files (leaving directories) created more than a year but it should skip one folder lets say “abc” for deletion purpose rest all can be deleted.

  2. Ritvij Vyas
    Ritvij Vyas says:

    This helped me out a lot. However, I need to delete all files and folders inside a particular directory without exception. The folders have odd file extensions, long names and some are just 1 KB.
    I used the below command
    forfiles /S /P E:\scratch /M * /D -30 /C “cmd /c if @isdir==TRUE rd /s /q @file

    But it seems to work sporadically just for some files inside the folder. Can you help me?

    • Jan Reilink
      Jan Reilink says:

      Hi Ritvij,

      The default searchmask (/M) already is *, you can leave that out. Your forfiles command should work as expected; it should delete all folders in E:\scratch that are older than 30 days.

      For testing purposes you can always echo: forfiles /S /P E:\scratch /M * /D -30 /C "cmd /c if @isdir==TRUE echo rd /s /q @file" to see what it does. And /q is nothing more than “quiet”, omit that one for more debug output.

  3. Jack
    Jack says:

    I can`t delete files which are located in more subdirectories. I have to give it full path and I have a lot of them!
    How can I give it just one path and it`ll search and delete in all sub-directories? I have only an extension type I have to delete. *.bb extension.

    • J. Reilink
      J. Reilink says:

      Hi Jack,
      Do you want to delete all sub directories, or all files with a .bb extension? Basically forfiles can do both, if you tell it to. For example, c:\temp has many subdirectories, and I want to delete all of them:

      forfiles /S /P C:\temp /C "cmd /c if @isdir==TRUE rd /s /q @file"

      To find and delete all files with a .bb extension with forfiles:

      forfiles /S /P C:\temp /M *.bb /C "cmd /c if @isdir==FALSE del @file"

      For testing purposes, you can always start with an echo command:

      forfiles /S /P C:\temp /M *.bb /C "cmd /c if @isdir==FALSE echo del @file"
    • Jan Reilink
      Jan Reilink says:

      Hello Aaron, thank you for your comment.

      With ForFiles, you can execute whatever command or script you like, so if you can script it you can execute it with ForFiles. But then I wouldn’t use ForFiles but something like PowerShell to do the job.


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *