Delete files in a folder older than N days

public void DeleteFilesFromFolder(string Folder_Path, int Days_Old)
{
//nav to folder path (Folder_Path passed in....must support both C:\  path  as well as UNC paths)
//delete all files older than Days_Old (passed in)
}


Please provide the source code for this function.  I don't do enough FSO to know how to do this fast.

I will research this on my own....if I win I'll request to PAQ the question....
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure what's going on there.  It's working on my system with this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            DeleteFilesFromFolder(FolderBrowserDialog1.SelectedPath, 10)
        End If
    End Sub

    Private Sub DeleteFilesFromFolder(ByVal Folder_Path As String, ByVal Days_Old As Integer)
        If Directory.Exists(Folder_Path) Then
            Dim s As String
            Dim dt As DateTime
            Dim ts As TimeSpan
            Dim date_diff As Double

            For Each s In Directory.GetFiles(Folder_Path)
                dt = File.GetCreationTime(s)
                ts = DateTime.Now().Subtract(dt)
                date_diff = ts.Days

                Debug.WriteLine(dt.ToString)
                Debug.WriteLine(date_diff)
            Next
        Else
            MessageBox.Show(Folder_Path, "Path Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
0
 
Tom KnowltonWeb developerAuthor Commented:
A beginning:

public void DeleteFilesFromFolder(string Folder_Path, int Days_Old)
                  {
                        foreach (string s in Directory.GetFiles(Folder_Path))
                        {
                              File.Delete(s);
                        }
                  }
0
 
Tom KnowltonWeb developerAuthor Commented:
I just need a way to determine the age of the files now.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
tdisessaCommented:
     File oThisDir = new File(Folder_Path);

      File [] aFilesInDir = oThisDir.listFiles();

      for (int iFile = 0; iFile < aFilesInDir.length; iFile++)
      {
         if (aFilesInDir[iFile].lastModified() + 1000 * 60 * 60 * 24 < System.currentTimeMillis())
         {
            aFilesInDir[iFile].delete();
         }
      }  
0
 
tdisessaCommented:
My solution is in Java.  What language are you using?
0
 
Tom KnowltonWeb developerAuthor Commented:
Sorry...C#.  :)
0
 
tdisessaCommented:
It started out looking like Java.
0
 
tdisessaCommented:
Oh well.  :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is in VB.Net but you should be able to convert it easily:

    Private Sub DeleteFilesFromFolder(ByVal Folder_Path As String, ByVal Days_Old As Integer)
        If Directory.Exists(Folder_Path) Then
            Dim fileAge As TimeSpan
            Dim fi As FileInfo
            Dim file As String
            Dim files() As String = Directory.GetFiles(Folder_Path)
            For Each file In files
                fi = New FileInfo(file)
                fileAge = DateTime.Now().Subtract(fi.CreationTime)
                If fileAge.Days > Days_Old Then
                    Try
                        fi.Delete()
                    Catch ex As Exception
                        MessageBox.Show(Folder_Path, "Delete Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Next
        Else
            MessageBox.Show(Folder_Path, "Path Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you don't want to use the creation date, then you can use either the last access or last write time:

    fileAge = DateTime.Now().Subtract(fi.LastAccessTime)
    fileAge = DateTime.Now().Subtract(fi.LastWriteTime)

~IM
0
 
Tom KnowltonWeb developerAuthor Commented:
what is fileage............ the difference in days?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
fileAge is an instance of the TimeSpan class, which represents the time spanned between two Date objects:

    Dim fileAge As TimeSpan
    fileAge = DateTime.Now().Subtract(fi.CreationTime)

You can get the time spanned in

    Ticks
    Milliseconds
    Seconds
    Mintutes
    Hours
    Days

Using the properties in the TimeSpan class.

~IM
0
 
Tom KnowltonWeb developerAuthor Commented:
Please help debug:


public void DeleteFilesFromFolder(string Folder_Path, int Days_Old)
                  {
                        foreach (string s in Directory.GetFiles(Folder_Path))
                        {
                              System.DateTime dt = File.GetCreationTime(s);
                              System.TimeSpan ts = System.DateTime.Now.Subtract(dt);

                              double date_diff = ts.Days;

                              if(date_diff >= Days_Old)
                              {
                                    File.Delete(s);
                              }
                        }
                  }


date_diff does not seem correct....says 0 when the days should be like 120 + days difference!
0
 
Tom KnowltonWeb developerAuthor Commented:
Turns out the file was hidden....I think that is why it wasn't working.

Tom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.