Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Directory.GetFiles Sort by Date

Tags:

asp.net

vb.net

I have a directory of Xml files. How do I grab only the files that have been created in the last 30 days and sort them in ascending order? I am not bound to using Directory.GetFiles if there is a more efficient solution.

I then create and bind a list of files to a gridView in which I need both the File Name and Path. I display the File Name and use the Path value in the RowDataBound event to build a HyperLink to the file.

Thanks! \m/ \m/

Dim filePaths() As String = Directory.GetFiles("C:\XmlFiles\")
Dim files As List(Of ListItem) = New List(Of ListItem)

For Each filePath As String In filePaths
    files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next

gv.DataSource = files
gv.DataBind()
like image 595
80sRocker Avatar asked Apr 24 '26 17:04

80sRocker


2 Answers

We can do a little better. Just getting the names and then sorting by time afterwards requires separate trips out the hard drive for both attributes, which is slow. By switching to DirectoryInfo, we can reduce this to one trip to disk per file:

Dim filesByDate = DirectoryInfo.EnumerateFiles("C:\XmlFiles\").
                     Where(Function(f) f.CreationTime > DateTime.Today.AddDays(-30)).
                     OrderBy(Function(f) f.CreationTime).
                     Select(Function(f) f.Name)
gv.DataSource = filesByDate
gv.DataBind()

Not that the difference is likely to drive your program's performance, but it's always nice to reduce disk I/O :)

This is also a case where it might be better to use GetX instead of EnumerateX. Generally, you want to prefer anything that will "enumerate" vs "get", because of the reduced memory use and the ability to start processing as soon as the first item is available, instead of waiting until the last item is available. In this case, though, there's a chance that you can trade memory use for disk I/O. I'm not familiar with the internal implementation of the methods, but it may be possible for GetFileSystemInfos() to get the information for all of the files in one or a few trips to disk, and that would be a big win. But again... I'm not familiar with the details here; you'd have to test for yourself which is better in your situation.

like image 185
Joel Coehoorn Avatar answered Apr 26 '26 06:04

Joel Coehoorn


You can use LINQ:

Dim filePathsSortedByDate = From f In Directory.EnumerateFiles("C:\XmlFiles\")
                            let fileCreationTime = File.GetCreationTime(f)
                            Where (Date.Today - fileCreationTime.Date).Days <= 30 
                            Order By fileCreationTime
                            Select New ListItem(Path.GetFileName(f), f)
Dim files As List(Of ListItem) = filePathsSortedByDate.ToList()
like image 28
Tim Schmelter Avatar answered Apr 26 '26 07:04

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!