Solved

Delete files older than dayofyear

Posted on 2016-09-16
2
43 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 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

734 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