Rem: Use Forfiles to Delete Old Files

In a large directory, I need to delete all the files over 1 year old. I know that I could use Robocopy's /MINAGE:n switch to move the old files to another directory, where I could delete them with the Del command. However, I was wondering whether there's a way to use Robocopy to delete the old files directly, without moving them first.

No, Robocopy doesn't have that capability, so you'll need to take another approach. The Dir command is useful for capturing the last modified date for a file. However, once you have that date, it's difficult to do the math to determine the file's age. You would have to write a lot of code to convert the date into a format that the command-shell math functions could use.

A better way is to use the Forfiles tool, which you can find in the Windows resource kits. Forfiles lets you perform a date-based deletion. Here's a sample command:

Forfiles -p R:\MyFiles -s -m *.*
  -d -365 -c "Cmd /C Echo
  0x22@Path\@File0x22"

(Although this command appears on several lines here, you'd enter it on one line in the command-shell window. The same holds true for the other multiline commands in this article.) In this command, the -p switch specifies the location in which to start searching, and the -s switch tells Forfiles to search subdirectories. You use the -m switch to tell Forfiles the types of files to search for. The default value of *.* tells Forfiles to search for files of all types. You use the -d switch to specify the age or age range of the files in which you're interested. The -d -365 switch tells Forfiles to search for files 365 days or older. Finally, you use the -c switch to tell Forfiles what to do with the files that meet your search criteria. For each file that meets the criteria, Forfiles will run the specified command. Note that you must enclose commands with spaces in quotes.

In the sample command, you might have noticed that the command Forfiles will run is displaying rather than deleting the files that are 1 year old or older. Any code that performs mass deletions or another potentially destructive operation must be tested extensively before implementing it in a production environment. I like to use the Echo command instead of the Del /q command in these situations. (The /q switch tells the Del command to delete files without confirmation.) That way, I see exactly what I'll be deleting.

For more information about Forfiles' switches and its usage possibilities, see the tool's online Help file. If you're interested in a deletion approach that uses a Perl script, check out "Real-World Scripting: Deleting Files by Their Age," June 2000, InstantDoc ID 8799.

Discuss this Article 26

Anonymous User (not verified)
on May 23, 2005
In the following -- forfiles -pH:\asdf -s -m*.* -d-1 -c"cmd /c del @path\@file" Try this --- Capitalize FORFILES @PATH and @FILE --- worked for me
Anonymous User (not verified)
on May 19, 2005
When I execute the code, it states "the system cannot find the file specified". But, when i do the 'forfiles -pC:\ -m*.blah" it finds the files I'm looking for. It just won't delete. Here is my complete coding..any help here?? """"forfiles -pH:\asdf -s -m*.* -d-1 -c"cmd /c del @path\@file" I also did the 0x22..and still nothing......???
Anonymous User (not verified)
on Mar 14, 2005
When I run forfiles like this (remove files older then 30 days in the C:\DELOLD directory): Forfiles -pC:\delold -m*.txt -d-30 -c"CMD /C del" I get this: Forfiles -pC:\delold -m*.txt -d-30 -c"CMD /C del" The syntax of the command is incorrect. I'm not quite sure what I'm doing wrong (tried various changes). Thank you
diego.busca@fastwebnet.it (not verified)
on Apr 12, 2005
You must put space after option and must use @file variable. The following line should be work Forfiles -p C:\delold -m *.txt -d -30 -c "CMD /C del @file"
Jon (not verified)
on Aug 13, 2008
Ugh... I'm having a hard time with this. Basically, this is my scenario. We have a Sharepoint server backing up hourly to a network directory. This directory is backed up to Backup Exec nightly. I need some command line utility that I can put in a batch file to delete folders and files in the original backup directory that are older than 1 day. Thanks in advance for your assistance.
CHRISTOPHER (not verified)
on Feb 4, 2005
Microsoft currently has a utility to delete bad mail. I can be downloaded free from http://www.microsoft.com/exchange/downloads/2003/default.mspx
smartcop (not verified)
on May 13, 2006
Are you sure the user can't use RoboCopy as stated? I think he is correct and it add's additonal possibilities. I'm using his method because it allows me to del files based upon Access Date ubnlike ForFiles which uses a different date. (creation or modify I'm not sure)
Anonymous User (not verified)
on Mar 2, 2005
Delete file name with space -You can use 0x22 as mentioned above this puts Quotes (") around your file name. On my version of forfiles its not accepting space after flags. e.g To delete files older then 100 days in c:\temp dir forfiles -pc:\temp -s -m*.* -d-100 -c"CMD /C del /q 0x22@PATH\@FILE0x22"
Anonymous User (not verified)
on Apr 20, 2005
I noticed that forfiles checks on the modification date, is there a possibility to check on the creation date?
wilzor
on Jan 26, 2005
Is it possible to use Forfiles to delete files in a directory with a space in the name? I'm trying to clear out a directory named "E:\exchsrvr\Mailroot\vsi 1\badmail". I cannot get Forfiles to work on this directory. Could you provide any ideas or workarounds? I've tried shares and lots more, but nothing works. Thanks much!
Jon (not verified)
on Aug 13, 2008
I'm completely new to the command FORFILES, so please excuse my questions. All I'm trying to do it to delete files and folders if they are over 1 day old. How do you get the directory folders themselves to delete? My batch file deletes the files, but leaves the folders. Please let me know what I'm doing wrong: FORFILES -p D:\Test -s -m *.* -d -1 -c "Cmd /C del /s /q *.* @PATH @ FILE
Anonymous User (not verified)
on Apr 12, 2005
May be you have to put a space between -p and the path
Anonymous User (not verified)
on Apr 19, 2005
Make sure your @FILE is in caps

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.