In this post I will explain how you can delete files in a directory that are older than x days,months,year,minute or second.In this post, I will explain to you, how you can delete files in a certain directory older than N months , days or year.
I’m working on a project in which I need to delete the files that are older than 60 days, in other words, files created less than 60 days ago should be kept, all others should be deleted.
For achieving this task we can follow the below code. I have created a simple console application and written the simple c# code for you.
You can delete the files using the property of FileSystemInfo class.you can delete the file using CreationTime of file or LastModifiedDate of file.
If you want to delete files in a directory using LastModifiedDate of the file then you have to use LastWriteTime property of FileSystemInfo class.
You can get LastModifiedDate of the file by property LastWriteTime of FileSystemInfo class.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DeleteOlderFile(60);
}
public static void DeleteOlderFile(int numberdays)
{
try
{
//get all files from folder
string dirName = System.Web.HttpContext.Current.Server.MapPath("/DownloadedContent");
string[] files = Directory.GetFiles(dirName);
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.CreationTime < DateTime.Now.AddMinutes(-numberdays))
fi.Delete();
}
}
catch (Exception ex)
{
}
}
}
}
You can also use 1 line Lamba Query:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DeleteOlderFileUsingLamda(60);
}
public static void DeleteOlderFileUsingLamda(int numberdays)
{
try
{
//get all files from folder
string dirName = System.Web.HttpContext.Current.Server.MapPath("/DownloadedContent");
Directory.GetFiles(dirName)
.Select(f => new FileInfo(f))
.Where(f => f.LastAccessTime < DateTime.Now.AddMonths(-numberdays))
.ToList()
.ForEach(f => f.Delete());
}
catch (Exception ex)
{
}
}
}
}
Points In this Post:-
- Delete file from folder using last modified property c#
- Delete Old Files in a Folder in C#
- Removing files that are older than some number of days
The post [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- [Simple Way]-Add/Delete table rows dynamically using JavaScript
- How to edit/delete selected row from HTML table using JavaScript
- Inline table editing with edit and delete icon in React Js
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Implementing “Remember Me” Feature in ASP.NET MVC
- [Solved]-Cookie loses value when page is changed in MVC
- Add edit and delete data in an HTML table using JavaScript
- How to add, edit and delete rows of an HTML table with Jquery?
- How to post File Upload and other form fields to one action Asp .Net MVC C#
- How To Post File and Data to API using HttpClient C#
- Sample AAC File Download For Testing
- [Solved]-How to call rest api from Excel macros vba and Parse Json
- Create ASP.NET Core Web API Without Entity Framework
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- React Js-Insert Update Display Delete CRUD Operations
- Instead Of Delete Trigger In Sql Server With Example
- [Solved]-How to Upload pdf file using jquery MVC?
- [Solved]-Uploading both data and files in FormData using Ajax MVC
- [Solved]-How to format Date Object in MM/DD/YYYY HH:MM:SS format using JavaScript ?
- [Solved]-Difference between two dates in minutes Javascript
- [Solved]-How to get days,minute, hours between two dates in JavaScript?
- [Solved]-How to add minutes in Date Object using javascript
- [Solved]-Download pdf file from link and save in local file folder in Asp .Net
- [Solved]-How Upload and Download PDF file Database in ASP.Net?
- [Solved]-Set,Delete and Read cookie value by name in JavaScript
- [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET
- Can we write Insert,update and delete queries in views?
- [Solved]-LEFT OUTER JOIN in LINQ With Where clause in C#
- [Solved]-Convert Base64 To Image And Save in folder and Display it- C#
- [Solved]-Convert an Image Url Into Base64 Data URL Using JavaScript With CORS
- [Solved]-How to Overlay Two Images in .NET-C#
- [Solved]-FFMPEG Converted Mp4 Video Not Working In HTML5-C#
- How To Check Whether File Exists or Not in Asp.Net ?
- Insert delete update records in CSV file -ASP.NET
- Insert, Update, Delete in Gridview using Single Stored Procedure
- Simple Way To Insert ,Update and Delete in Gridview-Asp.Net