Geeks With Blogs
Neil Smith [blog]code til ur fingers bleed

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.

Posted on Thursday, August 17, 2006 11:35 AM | Back to top


Comments on this post: Directory.GetFiles() in Modified Date/Time Order

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting 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!
Left by Lennard Fonteijn on Aug 30, 2006 3:59 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Perfect..
/Johan, Sweden
Left by Johan on Feb 07, 2007 11:30 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
many many thanks... this is exactly what i wanted.. should had post more in other forums.. cheers =D
Left by Yk on Feb 26, 2007 7:55 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks a lot, just what I was looking for.
Left by HImanshu on Jul 23, 2007 1:26 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks for sharing ^_^
Left by Eliseu Martiho on Aug 06, 2007 10:17 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
thanks a lot !
saved me a few hours of search !
Left by Elena on Aug 09, 2007 6:41 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thank you for sharing, exactly what i was looking for!!.
Left by Alex on Aug 29, 2007 3:49 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks for posting and saving me time!
Left by Robert on Apr 05, 2008 2:54 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks a lot
Left by shree on Apr 24, 2008 8:20 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Merci beaucoup !
Left by Cyril on Apr 29, 2008 9:13 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks! you save my time!!!
Left by Cyrus on Jul 02, 2008 2:49 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting 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
Left by nts on Jul 10, 2008 9:21 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
thank you
Left by ik2008 on Nov 15, 2008 7:05 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Sublime :)
Left by Kevin Crampton on Dec 02, 2008 2:28 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
This is what exactly I was looking for. Thanks a lot.
Left by Swapna on Dec 04, 2008 9:47 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
thanks a lot
Left by Orly on Feb 05, 2009 2:54 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks alot for sharing such a good stuff.
Left by Sohrab on Feb 15, 2009 10:01 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Many many thanks this is what exactly i want....
Left by KETAN DESHPANDE on Feb 16, 2009 5:52 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Can anyone please provide me the same vb.net code in c sharp. i am new in c sharp.

Thanks in advance.
Gaurav.
Left by Gaurav Gulati on Feb 25, 2009 4:14 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Hey Thanks a lot...
Left by Batul on Apr 13, 2009 2:57 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks a lot for this good code
Left by Jitu on Apr 13, 2009 11:59 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
perfect example. Thanks!!
Left by Lamar M on Apr 30, 2009 3:26 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
strategy pattern eh? was looking for exactly this :) thanks...
Left by dude on Jun 02, 2009 8:25 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
hi
I need get only the files of last one hours.

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

Claudinei
Left by Claudinei on Jun 25, 2009 8:56 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
cool thx a lot guys
Left by Mark on Dec 10, 2009 2:45 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
very nice ... with a little tweaking it can do many things. nice post
Left by 3 r ! c |< on Jan 28, 2010 1:29 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
thank you works fine !
Left by rudi on Feb 23, 2010 1:25 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Absolutely perfect solutions. Thanks.
Left by BUXjr on May 11, 2010 1:17 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks. It works perfect and helped me out great
Left by Yogesh Shah on Jun 02, 2010 6:55 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Perfect example for sort with customization.... Thanks a lot
Left by Haneef on Aug 26, 2010 4:24 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
This is perfect! tnx very much
Left by Eric Rotoni on Sep 16, 2010 10:35 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks a lot for sharing this information with the net

Very useful
Left by Ignacio on Nov 10, 2010 4:57 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
I've used LINQ successfully:
Dim f = From x In .GetFiles _ (dirinfo,FileIO.SearchOption.SearchTopLevelOnly, "*.xml") _
Order By .GetFileInfo(x).LastWriteTime Descending
Left by LostAtC on Dec 09, 2010 9:47 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting 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
Left by Gunju on May 09, 2011 2:55 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Can you please get me the equivalent in VB6
Thanks
Left by Arvind on Jun 23, 2011 7:33 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Excellent code. Worked very well. Thanks!!
Left by ibjokn on Oct 03, 2011 12:06 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks, worked very well!
Left by Timmy on Oct 05, 2011 10:23 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thanks. Very usefull
Left by Paco on Oct 14, 2011 11:37 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting 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
Left by Will Robinson on Jan 05, 2012 10:00 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Awesome solution! Excellent and efficent means of parsing through large folders to sort properly - useful for my little backup routine. Thanks for sharing!
Left by Matt on Jan 06, 2012 7:11 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting 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!
Left by Renato Aloi on Jan 13, 2012 6:25 AM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
This is exactly what I needed to figure out. thanks a bunch!
Left by Kburnett on Jun 26, 2013 3:46 PM

# re: Directory.GetFiles() in Modified Date/Time Order
Requesting Gravatar...
Thank you for sharing with the rest. It works...
Left by Guayabo626 on Dec 11, 2013 1:11 PM

Your comment:
 (will show your gravatar)


Copyright © Neil Smith | Powered by: GeeksWithBlogs.net | Join free