Solved

Delete files older than dayofyear

Posted on 2016-09-16
2
30 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
2 Comments
 
LVL 35

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now