Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

January 13, 2005 12:00 AM

Rem: Use Forfiles to Delete Old Files

Windows IT Pro
InstantDoc ID #44891
Rating: (13)

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.

Related Content:

ARTICLE TOOLS

Comments
  • Jon
    4 years ago
    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.

  • Jon
    4 years ago
    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

  • smartcop
    6 years ago
    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
    7 years ago
    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
    7 years ago
    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......???

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.