Delete old files

mpires

Member
Joined
Jan 29, 2008
Messages
17
Programming Experience
Beginner
Hi all,

I have a folder that have in there allot of log files and i need every time i run one vb.net app to clear that folder and live the 3 last logs only, i was thinking in read file by file and get file proprieties and after read all files store the 3 last creation date and clear the others, but at same time i think this will be to much complicate, cold any one give one idea how cold i simplify this lol

This log's are made randomly they are not made Avery day...

i'm developing in vb

Cheers
 
There are various ways you could do it. Here's my recommendation. Write this method:
VB.NET:
Private Function CompareFileInfosByDescendingCreationTime(ByVal file1 As IO.FileInfo, _
                                                          ByVal file2 As IO.FileInfo) As Integer
    Return -Date.Compare(file1.CreationTime, file2.CreationTime)
End Function
which will be used to sort an array of FileInfo objects. Use it like this:
VB.NET:
Dim files As IO.FileInfo() = (New IO.DirectoryInfo("folder path here")).GetFiles("*.log")

If files.Length > 3 Then
    Array.Sort(files, New Comparison(Of IO.FileInfo)(AddressOf CompareFileInfosByDescendingCreationTime))

    For index As Integer = 3 To files.GetUpperBound(0) Step 1
        files(index).Delete()
    Next index
End If
The files are sorted in descending order by creation time and then all but the first three, i.e. the three most recent files, are deleted.
 
Like I said, there are various ways you could do it, which really means there are various ways you could sort the array. Another option would be:
VB.NET:
Dim files As IO.FileInfo() = (New IO.DirectoryInfo("folder path here")).GetFiles("*.log")

If files.Length > 3 Then
    Dim upperBound As Integer = files.GetUpperBound(0)
    Dim dates(upperBound) As Date

    For index As Integer = 0 To upperBound Step 1
        dates(index) = files(index).CreationTime
    Next index

    Array.Sort(dates, files)
    Array.Reverse(files)

    For index As Integer = 3 To upperBound Step 1
        files(index).Delete()
    Next index
End If
Personally, I like using the Comparison delegate.
 
Yes it works to,

But these 2 options are the same correct they will set on array list always...

I'm trying to understand the code.

Mpires
 
The two examples do the same thing, differing only in how the array of FileInfo objects is sorted. The Array.Sort has several overloads, allowing you to sort an array in several different ways.

The first example specifies the array to sort:
VB.NET:
Array.Sort([B][U]files[/U][/B], New Comparison(Of IO.FileInfo)(AddressOf CompareFileInfosByDescendingCreationTime))
and a Comparison delegate:
VB.NET:
Array.Sort(files, [B][U]New Comparison(Of IO.FileInfo)(AddressOf CompareFileInfosByDescendingCreationTime)[/U][/B])
The Comparison delegate is basically a reference to a method that can be used to compare any two elements of the array and return a value indicating their relative order. If the method returns a value less than zero then the first object comes before the second, if the method returns a value greater than zero the second element comes before the first and if the method returns zero the are equivalent. Our Comparison delegate refers to the CompareFileInfosByDescendingCreationTime method, which uses the Date.Compare method to compare the CreationTime properties of the two FileInfo objects. The value returned by Date.Compare is then negated so the resulting order will be descending instead of ascending.

The second example specifies the array to sort:
VB.NET:
Array.Sort(dates, [B][U]files[/U][/B])
and an array of keys to sort by:
VB.NET:
Array.Sort([B][U]dates[/U][/B], files)
Internally the first array will be sorted using its default comparison, which will actually be Date.Compare as well, and the second array will be placed into the same corresponding order. The array is then reversed to make the order descending instead of ascending.
 
Back
Top