Query to search the older files in a folder and delete them using LINQ

Prabhukiran345
Posted by Prabhukiran345 under LINQ category on | Points: 40 | Views : 493
var query = from o in Directory.GetFiles("/YourFolder", "*.*",
SearchOption.AllDirectories)
let x = new FileInfo(o)
where x.CreationTime <= DateTime.Now.AddMonths(-6)
select o;
foreach (var item in query)
{
File.Delete(item);
}


This will delete all files older than 6 month from current datye from the Specified folder.

Comments or Responses

Login to post response