2

how to delete files which are more than one month old using c# script. i am using framework 2.0..

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
SAK
  • 3,780
  • 7
  • 27
  • 38
  • Can you give us some more info, delte them? what for? You want to scan your entire pc for files over 1 month old and delete them all ? – Nealv Jul 27 '10 at 13:26

2 Answers2

8
string path = @"C:\Temp\"; //"

DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] fileInfos = dirInfo.GetFiles();

foreach (FileInfo fileInfo in fileInfos)
{
    if (fileInfo.LastWriteTime < DateTime.Now.AddMonths(-1))
        fileInfo.Delete();
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
3

You can call Directory.GetFiles to find all files in a folder.
You can call File.GetLastWriteTime to check when the file was modified.
You can call File.Delete to delete a file.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964