Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to automatically delete files older than X in a folder

Posted on 2010-11-30
2
Medium Priority
?
398 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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