Delete All PDF files (*.pdf) from a folder older than one day

kgovindarao523-21772
Posted by kgovindarao523-21772 under C# category on | Points: 40 | Views : 1114
Hi,
I want to delete all pdf files were created longer than one day.
Here is the code.
string[] files = Directory.GetFiles(strFolderPath);//Folder path ex: .GetFiles(@"C:\Test")
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
//you can change the condition based on your requirement for ex.for 5 days or only .docx files
if (fi.CreationTime.Date < DateTime.Now.AddDays(-1) && fi.FullName.Contains(".pdf"))
fi.Delete();
}

Comments or Responses

Login to post response