Using Directory.GetFiles() WITH multiple extensions AND sort order

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Hi,

I have to get a directory file list, filtered on multiple extensions...and sorted!

I use this, which is the fastest way I've found to get dir content filtered on multiple extensions:

VB.NET:
Dim ext As String() = {"*.jpg", "*.bmp","*png"}
Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)).ToArray
Array.Sort(files)

and then use an array sort.


I was wondering (and this is my question ;)) if there would be a way to do the sorting IN the same main line? A kind of:
VB.NET:
Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f)[B].Order By Name[/B]).ToArray

and, if yes, if I would gain speed doing this instead of sorting the array at the end (but I would do my test and report..as soon as I get a solution!!)?
Thanks for your help!!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
In Linq you have the OrderBy extension method, or the Order By clause in an expression. There would be nothing to gain in speed, because you have multiple GetFiles calls that all must complete before sorting can take place. Last file returned could be first in sort order.
 

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Thanks John!
You're right, using
VB.NET:
myFiles = myExtensions.SelectMany(Function(ext) Directory.GetFiles(myPath, ext)).OrderBy(Function(x) x).ToArray
..is insignificant!

On 20000 files, an average of 0.17 sec instead of and average of 0.18 sec !!

Thanks!!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Faster could be to only call GetFiles once and filter results by file extension.
 

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
You're right again...especially when the number of extensions to look for is raising!

VB.NET:
Dim supportedExtensions As String = "*.zip,*.aaa,*.bbb,*.ccc,*.ddd"
Dim files As String() = Directory.GetFiles(romPath, "*.*", SearchOption.AllDirectories)
Array.Sort(files)

For Each fi As String In files
 If supportedExtensions.Contains(Path.GetExtension(fi)) Then
 ...
 End If
Next

...gives invariably the same amount of time whatever the number of extension is...which is not the case of my previous code.
In my case, on 20000 files, 6 extension types: 0.2sec for this method against 0.6sec for the previous one!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
You can save both time and memory by filtering the results before you sort them. For example:
        Dim ext = {".jpg", ".bmp", ".png"}
        Dim dir As New IO.DirectoryInfo(romPath)
        Dim files = From file In dir.GetFiles("*.*", IO.SearchOption.AllDirectories)
                    Where ext.Contains(file.Extension)
                    Order By file.Name
                    Select file.FullName
 

giorgiogio48

New member
Joined
Jul 28, 2022
Messages
1
Programming Experience
3-5
You can save both time and memory by filtering the results before you sort them. For example:
        Dim ext = {".jpg", ".bmp", ".png"}
        Dim dir As New IO.DirectoryInfo(romPath)
        Dim files = From file In dir.GetFiles("*.*", IO.SearchOption.AllDirectories)
                    Where ext.Contains(file.Extension)
                    Order By file.Name
                    Select file.FullName
Very good! It works excellently. Goodbye
 
Top Bottom