1

I have a C# application in which there is a requirement to delete all the files whose date created will be older than today’s date. How can we achieve this?

I had a search in google to find out a sample code. So I decide to create one and post it here.If you want to delete older files from a directory , you can use below method. If you have any queries on this please let me know as well, I am very happy to help you.

public void FolderDelete()
            {

                DirectoryInfo d = new DirectoryInfo(ConfigurationManager.AppSettings["<path>"]);
                if (d.Exists)
                {
                  //Get all Directories from the path
                  string[] folders = Directory.GetDirectories(d.ToString());
                    foreach (var item in folders)
                    {
                        DirectoryInfo info = new DirectoryInfo(item);
                        //It will purge the 14 days older directories 
                        if (info.CreationTime < DateTime.Now.AddDays(-14))
                        {
                            info.Delete(true);
                        }
                    }
                }


                else
                {
                    Console.WriteLine("There is no 14 days older files.");
                }

            }

This is very useful and simple to use.I have tried the above method.Could any one of you have another idea, Please share with me.

Arun P P
  • 19
  • 2
  • 1
    Consider making a *Q&A style question* the next time, unless you are explicitly asking for a better or more elegant solution. – Nolonar Aug 07 '13 at 12:18
  • 2
    That write to console is a bit deceiving since it actually is called when the directory doesn't exist. – Kevin DiTraglia Aug 07 '13 at 12:19
  • This turned out to be handy. Just surprised there wasn't a Linq style way of doing this. Edit: Actually found something that was used for files, but will work for directories as well. Will post that answer. – ouflak Feb 13 '14 at 16:33

2 Answers2

0

If you have windows, forfiles may be what you want.

http://technet.microsoft.com/en-us/library/cc753551(WS.10).aspx

lordkain
  • 3,061
  • 1
  • 13
  • 18
0

Found a Linq way of doing this...

  DirectoryInfo d = new DirectoryInfo(ConfigurationManager.AppSettings["<path>"]);

  List<string> recentDirectories = d.GetDirectories()
                                        .Select(x => x.EnumerateDirectories()
                                                    .OrderByDescending(f => DateTime.Now.AddDays(-14))
                                                    .FirstOrDefault())
                                        .Where(x => x != null)
                                        .Select(x => x.FullName)
                                        .ToList();

  foreach (string dir in recentDirectories)
  {
      Directory.Delete(dir);
  }

This is based on an answer to this question here.

Here is the non-Linq way of doing the equivalent:

   DirectoryInfo[] testDirs = d.GetDirectories();
   List<DirectoryInfo> dirInfos = d.EnumerateDirectories().ToList();
   IOrderedEnumerable<DirectoryInfo> infos = dirInfos.OrderByDescending(f => DateTime.Now.AddDays(-14));
   dirInfos = infos.ToList();

   foreach (DirectoryInfo dir in dirInfos)
   {
       Directory.Delete(dir.FullName);
   }
Community
  • 1
  • 1
ouflak
  • 2,458
  • 10
  • 44
  • 49