Delete files older than 3 months old in a directory using .NET

I would like to know (using C#) how I can delete files in a certain directory older than 3 months, but I guess the date period could be flexible.

Just to be clear: I am looking for files that are older than 90 days, in other words files created less than 90 days ago should be kept, all others deleted.

Something like this outta do it.

using System.IO; 

string[] files = Directory.GetFiles(dirName);

foreach (string file in files)
{
   FileInfo fi = new FileInfo(file);
   if (fi.LastAccessTime < DateTime.Now.AddMonths(-3))
      fi.Delete();
}

C# - Delete files older than N number of Days/Months, Delete Old Files in a Folder in C# – Asp.Net DateTime.AddDays() Method - Delete Files Older Than One Day. asp.net A few months back I have designed an Online Image optimizer tool, and named it Image Resizer. It's a free online tool . Answers: Something like this outta do it. using System.IO; string [] files = Directory.GetFiles (dirName); foreach (string file in files) { FileInfo fi = new FileInfo (file); if (fi.LastAccessTime < DateTime.Now.AddMonths (-3)) fi.Delete (); } Questions:

Here's a 1-liner lambda:

Directory.GetFiles(dirName)
         .Select(f => new FileInfo(f))
         .Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-3))
         .ToList()
         .ForEach(f => f.Delete());

Delete Old Files in a Folder in C# – Asp.Net DateTime.AddDays , I would like to know (using C#) how I can delete files in a certain directory older than 3 months, but I guess the date period could be flexible. Just to be clear: I am � Timeline for Delete files older than 3 months old in a directory using .NET Current License: CC BY-SA 3.0. 24 events when toggle format

For those that like to over-use LINQ.

(from f in new DirectoryInfo("C:/Temp").GetFiles()
 where f.CreationTime < DateTime.Now.Subtract(TimeSpan.FromDays(90))
 select f
).ToList()
    .ForEach(f => f.Delete());

Delete files older than 3 months old in a directory using .NET , If You want to Temp or your specific Export file History maintain folder to delete file after some days then this function resolve your problem. Hi all, I want to delete all files in a directory which are older than a given date. I thought of doing it by creating a file by the required date by using touch command. And then i would use find command on that file and try to find files older than that. I searched the man and found a (3 Replies)

Here's a snippet of how to get the creation time of files in the directory and find those which have been created 3 months ago (90 days ago to be exact):

    DirectoryInfo source = new DirectoryInfo(sourceDirectoryPath);

    // Get info of each file into the directory
    foreach (FileInfo fi in source.GetFiles())
    {
        var creationTime = fi.CreationTime;

        if(creationTime < (DateTime.Now- new TimeSpan(90, 0, 0, 0)))
        {
            fi.Delete();
        }
    }

Delete files older than 3 months old in a directory using .NET, Something like this outta do it. using System.IO; string[] files = Directory.GetFiles( dirName); foreach (string file in files) { FileInfo fi = new� .net powershell powershell-3.0. Delete files older than 3 months old in a directory using .NET. 954. Delete files older than 15 days using PowerShell.

The GetLastAccessTime property on the System.IO.File class should help.

Delete files older than 3 months old in a directory using .NET, 3 Shares. Windows 10 has built-in features to free up space by deleting old files in Also, the Disk Cleanup tool clears up files in the %temp% directory that Delete Files that are Older than 'N' days Using File Explorer For example,-365 means over a year ago, -30 means a month ago. NET USE \\coresrv\Software / D. For example, to remove the files older than a month from the Downloads folder, use the following command: ForFiles /p "%userprofile%\Downloads" /s /d -30 /c "cmd /c del @file". This trick works in all modern versions of Windows including Windows 7, Windows 8, Windows 8.1 and Windows 10.

How to Delete Files Older than N Days Automatically in Windows , Delete the files in a specific folder older than a months using SSIS Script Task (C #). Folder has files with different name. Just based upon the� - Represents the current directory.-mtime - Represents the file modification time and is used to find files older than 30 days.-print - Displays the older files; If you want to search files in a specific directory, just replace the dot with the folder path.

Delete files older than a month using C# in SSIS - MSDN, Get code examples like "c# delete files older than x months" instantly using System.IO;. 2. . 3. string[] files = Directory.GetFiles(dirName);. 4. 1. Delete Files Older Than 30 Days. This command will delete all files older than 30 days in system /opt/backup directory. find /opt/backup -type f -mtime +30 -exec rm -f {} \; 2. Delete Files Older Than 30 Days with .log Extension. If you want to delete only specified extension files, you can use the following command.

How to delete files in a folder when the date is in the name of the file?, I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results. The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.

Comments
  • If there is a important amount of files, the best is to use EnumerateFiles and EnumerateDirectories instead of GetFiles and GetDirectories, because they directly run the enumeration instead of gathering a list. However, you will have to use a foreach loop.
  • Thanks, I noticed you're using lastAccessTime, is this the creation time?
  • no, as propertyNames says: LastAccessTime - you should go for property CreationTime if you'd like to!
  • Yeah, which property you use is entirely up to you. You could also use LastWriteTime if you wanted.
  • +1 for helping me out. Instead of creating a new FileInfo instance you could use File.GetCreationTime or File.GetLastAccessTime. Should be a minor performance improvement.
  • I guess GetFiles and Delete never fail in your environment? Just pointing that out since this seems to be a highly referenced answer.
  • @VladL I get a "IEnumerable<FileInfo> does not contain ForEach" if I drop the ToList(). I just kept it in.
  • I like this. But I would wrap the delete in a try/catch
  • new DirectoryInfo(dir).GetFiles() is faster than new FileInfo(f) for every single file.
  • var filesToDelete = new DirectoryInfo(@"C:\Temp").GetFiles().Where(x=>x.LastAccessTime < DateTime.Now.AddMonths(-3)); //variation
  • Woho! Someone else than me thinks over-using LINQ is awesome! ;)
  • What does the .ToList() call add other than a second loop through the matched files?
  • @Joel Mueller. List<T> defines a ForEach method which can be used to apply an Action<T> to all elements. Unfortunately there is no such extension method for IEnumerable<T>.