Directory.GetFiles() in Modified Date/Time Order

I’ve been forced in to using vb.net for a windows service project which scans a folder of xml files, they need to be processed in order of the files modified date & time.  Directory.GetFiles() returns an array of filenames in alphabetic order, I could find precious little information on the net so I thought I’d share what I came up with :-

 

Imports System.IO

Public Class clsCompareFileInfo

    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

        Dim File1 As FileInfo

        Dim File2 As FileInfo

 

        File1 = DirectCast(x, FileInfo)

        File2 = DirectCast(y, FileInfo)

 

        Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)

    End Function

End Class

 

Then to use it…

 

Dim dirinfo As DirectoryInfo

Dim allFiles() As FileInfo

 

dirinfo = New DirectoryInfo(sSelPath)

allFiles = dirinfo.GetFiles("*.xml")

Array.Sort(allFiles, New clsCompareFileInfo)

For Each fl As FileInfo In allFiles

    MessageBox(fl.FullName.ToString())

Next

 

To give credit where it’s really due I found the solution in c# at http://www.shanebauer.com/Weblog/PermaLink,guid,b9cd286d-e147-4a25-aa49-bde888a11433.aspx. I just converted the source from c# to vb.net.

Print | posted on Thursday, August 17, 2006 11:35 AM

Feedback

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Lennard Fonteijn at 8/30/2006 3:59 PM
Gravatar Thank you, really needed this :)

"I could find precious little information on the net"

Had exact the same problem, till i found your post.

Thanks again!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Johan at 2/7/2007 11:30 AM
Gravatar Perfect..
/Johan, Sweden

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Yk at 2/26/2007 7:55 AM
Gravatar many many thanks... this is exactly what i wanted.. should had post more in other forums.. cheers =D

# re: Directory.GetFiles() in Modified Date/Time Order

Left by HImanshu at 7/23/2007 1:26 PM
Gravatar Thanks a lot, just what I was looking for.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Eliseu Martiho at 8/6/2007 10:17 AM
Gravatar Thanks for sharing ^_^

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Elena at 8/9/2007 6:41 AM
Gravatar thanks a lot !
saved me a few hours of search !

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Alex at 8/29/2007 3:49 PM
Gravatar Thank you for sharing, exactly what i was looking for!!.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Robert at 4/5/2008 2:54 PM
Gravatar Thanks for posting and saving me time!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by shree at 4/24/2008 8:20 PM
Gravatar Thanks a lot

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Cyril at 4/29/2008 9:13 PM
Gravatar Merci beaucoup !

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Cyrus at 7/2/2008 2:49 PM
Gravatar Thanks! you save my time!!!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by nts at 7/10/2008 9:21 PM
Gravatar these struggle if more than one file is created per second, updated post here http://geekswithblogs.net/ntsmith/archive/2008/07/10/123732.aspx

# re: Directory.GetFiles() in Modified Date/Time Order

Left by ik2008 at 11/15/2008 7:05 PM
Gravatar thank you

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Kevin Crampton at 12/2/2008 2:28 AM
Gravatar Sublime :)

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Swapna at 12/4/2008 9:47 PM
Gravatar This is what exactly I was looking for. Thanks a lot.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Orly at 2/5/2009 2:54 AM
Gravatar thanks a lot

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Sohrab at 2/15/2009 10:01 PM
Gravatar Thanks alot for sharing such a good stuff.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by KETAN DESHPANDE at 2/16/2009 5:52 AM
Gravatar Many many thanks this is what exactly i want....

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Gaurav Gulati at 2/25/2009 4:14 AM
Gravatar Can anyone please provide me the same vb.net code in c sharp. i am new in c sharp.

Thanks in advance.
Gaurav.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Batul at 4/13/2009 2:57 AM
Gravatar Hey Thanks a lot...

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Jitu at 4/13/2009 11:59 PM
Gravatar Thanks a lot for this good code

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Lamar M at 4/30/2009 3:26 AM
Gravatar perfect example. Thanks!!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by dude at 6/2/2009 8:25 PM
Gravatar strategy pattern eh? was looking for exactly this :) thanks...

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Claudinei at 6/25/2009 8:56 AM
Gravatar hi
I need get only the files of last one hours.

For example;
Dir.getfile("*.txt", file.hour < now(-1))

Claudinei

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Mark at 12/10/2009 2:45 AM
Gravatar cool thx a lot guys

# re: Directory.GetFiles() in Modified Date/Time Order

Left by 3 r ! c |< at 1/28/2010 1:29 AM
Gravatar very nice ... with a little tweaking it can do many things. nice post

# re: Directory.GetFiles() in Modified Date/Time Order

Left by rudi at 2/23/2010 1:25 PM
Gravatar thank you works fine !

# re: Directory.GetFiles() in Modified Date/Time Order

Left by BUXjr at 5/11/2010 1:17 PM
Gravatar Absolutely perfect solutions. Thanks.

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Yogesh Shah at 6/2/2010 6:55 AM
Gravatar Thanks. It works perfect and helped me out great

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Haneef at 8/26/2010 4:24 AM
Gravatar Perfect example for sort with customization.... Thanks a lot

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Eric Rotoni at 9/16/2010 10:35 PM
Gravatar This is perfect! tnx very much

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Ignacio at 11/10/2010 4:57 PM
Gravatar Thanks a lot for sharing this information with the net

Very useful

# re: Directory.GetFiles() in Modified Date/Time Order

Left by LostAtC at 12/9/2010 9:47 AM
Gravatar I've used LINQ successfully:
Dim f = From x In .GetFiles _ (dirinfo,FileIO.SearchOption.SearchTopLevelOnly, "*.xml") _
Order By .GetFileInfo(x).LastWriteTime Descending

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Gunju at 5/9/2011 2:55 AM
Gravatar Actually I am redaing the files from array and storing in table and sorting the table view based on creation time.

My code goes like this:
Dim aryFileinfo As IO.FileInfo() = lfolderinformation.GetFiles(drpIP.SelectedItem.Value & strFileExtension)
'Inner For loop - Getting File Contents
For Each lfileinformation In aryFileinfo
'Dim lfilecreationtime As String = lfileinformation.CreationTime
If lfileinformation.Extension = strFileExtension Then
If lfileinformation.Length > 1024 Then
strFileSize = Format((lfileinformation.Length / 1024), "#,###.00") & " KB"
Else
strFileSize = lfileinformation.Length & " Bytes"
End If
row = table.NewRow()
row("File Name") = lfileinformation.Name
row("Backup File Path") = lfileinformation.FullName
row("File Creation Time") = lfolderinformation.CreationTime
row("Last Accessed") = lfileinformation.LastAccessTime
row("File Size") = strFileSize
table.Rows.Add(row)
End If
Next
Next
If table.Rows.Count > 0 Then
pnlResult.Visible = True

table.DefaultView.ApplyDefaultSort = "[File Creation Time] DESC"

if I am running it locally, its working but if I am deploying in production environment..Its not sorting fully...Please suggest

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Arvind at 6/23/2011 7:33 AM
Gravatar Can you please get me the equivalent in VB6
Thanks

# re: Directory.GetFiles() in Modified Date/Time Order

Left by ibjokn at 10/3/2011 12:06 PM
Gravatar Excellent code. Worked very well. Thanks!!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Timmy at 10/5/2011 10:23 AM
Gravatar Thanks, worked very well!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Paco at 10/14/2011 11:37 AM
Gravatar Thanks. Very usefull

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Will Robinson at 1/5/2012 10:00 PM
Gravatar Bit better with the type safety if you use generics - saves you having to call DirectCast, too :)

Public Class clsCompareFileInfo
Implements IComparer(Of IO.FileInfo)

Public Function Compare(x As System.IO.FileInfo, y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
Return DateTime.Compare(x.LastWriteTime, y.LastWriteTime)
End Function
End Class

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Matt at 1/6/2012 7:11 AM
Gravatar Awesome solution! Excellent and efficent means of parsing through large folders to sort properly - useful for my little backup routine. Thanks for sharing!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Renato Aloi at 1/13/2012 6:25 AM
Gravatar Thanks for sharing! It works! And if you want to order ascending or descending just swap object_x by object_y or file1 by file2. Sweet!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Kburnett at 6/26/2013 3:46 PM
Gravatar This is exactly what I needed to figure out. thanks a bunch!

# re: Directory.GetFiles() in Modified Date/Time Order

Left by Guayabo626 at 12/11/2013 1:11 PM
Gravatar Thank you for sharing with the rest. It works...

Your comment:





 

Copyright © Neil Smith

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski