Solved

Delete files older than dayofyear

Posted on 2016-09-16
2
47 Views
Last Modified: 2016-09-16
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()
        Try
            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.AddRange(di.GetFiles())
            files.Sort(AddressOf SortFilesDescendingByLastWriteTime)
            Dim newestFile As System.IO.FileInfo = files(0)
            Dim dtYear = newestFile.LastWriteTime.DayOfYear
        Catch ex As Exception
            MsgBox(ex.Message)
        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

Open in new window

0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 500 total points
ID: 41801443
It would work something like this:

Private Sub getFiles()
        Try
                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.AddRange(di.GetFiles())
            files.Sort(AddressOf SortFilesDescendingByLastWriteTime)
            Dim newestFile As System.IO.FileInfo = files(0)
            Dim dtYear = newestFile.LastWriteTime.DayOfYear
            Dim DeleteDate = DateAdd(DateInterval.Day, -2, newestFile.LastWriteTime)
            MsgBox(dtYear & "-" & DeleteDate)
            For Each file In files
                If file.LastWriteTime < DeleteDate Then
                    file.Delete()
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window


I didn't reference your dtYear, as when programming and debugging, it causes more headache than just keeping it as a true date.
There might be some minor inconsistencies, which you'll probably able to fix (such as using the difference as -2 or -3, and fixing it the cutoff time to the file date, or 00.00)
0
 

Author Closing Comment

by:lrbrister
ID: 41801455
Perfect... thanks.

Watch for follow-up
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

632 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