Solved

How to automatically delete files older than X in a folder

Posted on 2010-11-30
2
389 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
  • 2
2 Comments
 
LVL 74

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 74

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now