Solved

How to automatically delete files older than X in a folder

Posted on 2010-11-30
2
393 Views
Last Modified: 2013-11-07
We have a .net program and we create various log files and want to delete them after a certain amount of time. What is the best way to do this?
0
Comment
Question by:jamesh1031
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34241893
You can loop through the directory and check the time against a target value. For example, if you wanted to delete those older than 30 days, you could use:
public static void PurgeDirectory(string path)
{
    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(path);
    DateTime targetDate = DateTime.Now.AddDays(-30);

    foreach (System.IO.FileInfo fi in info.GetFiles())
    {
        if (fi.LastAccessTime > targetDate)
        {
            fi.Delete();
        }
    }
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34241948
If you use the above, you should be a bit more cautious by checking that the path exists before trying to get its files:
public static void PurgeDirectory(string path)
{
    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(path);

    if (info.Exists)
    {
        DateTime targetDate = DateTime.Now.AddDays(-30);

        foreach (System.IO.FileInfo fi in info.GetFiles())
        {
            if (fi.LastAccessTime > targetDate)
            {
                fi.Delete();
            }
        }
    }
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question