Windows Server logo

Delete files recursively with ForFiles in Windows Server

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 or del command on all files in a tree with the .txt extension.

Home » Delete files recursively with ForFiles in Windows Server

In this post you’ll learn how to delete files recursively with Forfiles.exe on Windows Server, for example deleting recursive if files are older than ‘n’ days (where n is 3, 5 or 10 days for example).

With Forfile.exes, you can run a command on or pass arguments to multiple files. For example, you could run the type or del command on all files in a tree with the .txt extension.

The Forfilessyntax is:

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

ForfilesWindows 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.exe, you can easily batch delete files older than 7 days in Windows and Windows Server.

Forfiles batch process and delete files older than 'n' days

As said, you can easily delete files older than 3 days with forfiles. Here is 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.

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.

More Windows forfiles command tips

Another great forfilesto list files older than 3 days

forfiles /P Z:\devops\sites\example.org /M *.* /D -3 /C "cmd /c dir @FILE"

This command finds all files in the directory Z:\devops\sites\example.orgare older than 3 days, and does a diron those files.

Again /Pthe path to perform the command in, /Ma searchmask (*.*everything), /Dthe last modified date, and /Ccommand to perform against @FILE.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments