Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete files in a folder older than N days

Posted on 2004-10-28
14
Medium Priority
?
1,794 Views
Last Modified: 2012-08-13
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....
0
Comment
Question by:Tom Knowlton
  • 6
  • 4
  • 4
14 Comments
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12435609
A beginning:

public void DeleteFilesFromFolder(string Folder_Path, int Days_Old)
                  {
                        foreach (string s in Directory.GetFiles(Folder_Path))
                        {
                              File.Delete(s);
                        }
                  }
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12435614
I just need a way to determine the age of the files now.
0
 
LVL 2

Assisted Solution

by:tdisessa
tdisessa earned 200 total points
ID: 12435715
     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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:tdisessa
ID: 12435734
My solution is in Java.  What language are you using?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12435751
Sorry...C#.  :)
0
 
LVL 2

Expert Comment

by:tdisessa
ID: 12435768
It started out looking like Java.
0
 
LVL 2

Expert Comment

by:tdisessa
ID: 12435776
Oh well.  :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12436272
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12436300
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
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12436336
what is fileage............ the difference in days?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12436375
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
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12436497
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1800 total points
ID: 12436621
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
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12437256
Turns out the file was hidden....I think that is why it wasn't working.

Tom
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question