troubleshooting Question

Delete files older than dayofyear

Avatar of Larry Brister
Larry BristerFlag for United States of America asked on
Visual Basic.NET.NET ProgrammingC#
2 Comments1 Solution86 ViewsLast Modified:
In my code below I am getting the newest file in a folder
Then setting day of year variable(dtYear) from that files last write time

What I would like to do is delete all files in that folder OLDER that dtYear - 2
So that I always have three days of files in the folder.

Private Sub getFiles()
            Dim appPath As String = Application.StartupPath
            Dim strPath As String
            strPath = File.ReadAllLines(Path.GetFullPath("directorypath.txt")).First() '"C:\Program FIles\"
            Dim di As New System.IO.DirectoryInfo(strPath)
            Dim files As New List(Of System.IO.FileInfo)
            files.Sort(AddressOf SortFilesDescendingByLastWriteTime)
            Dim newestFile As System.IO.FileInfo = files(0)
            Dim dtYear = newestFile.LastWriteTime.DayOfYear
        Catch ex As Exception
        End Try
    End Sub

    Private Function SortFilesDescendingByLastWriteTime(ByVal fiA As System.IO.FileInfo, ByVal fiB As System.IO.FileInfo) As Integer
        Return fiB.LastWriteTime.CompareTo(fiA.LastWriteTime)
    End Function
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros