#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    29
    Rep Power
    0

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


    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:

    Code:
    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:
    Code:
    Dim files As String() = ext.SelectMany(Function(f) Directory.GetFiles(romPath, f).Order By Name).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!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    The "Dir" command contains a number of sort options. If one of those options fits your purpose, you could use the "Shell" command to output the sorted list to a file, and then read the file.

    Shell "cmd.exe /c Dir *.jpg, *.bmp, *.png /o:d > DirList.txt"

    This sorts the files by date.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    29
    Rep Power
    0
    Ah yes, that could be an idea!!

    At the moment I've tested a :
    Code:
    myFiles = myExtensions.SelectMany(Function(ext) Directory.GetFiles(myPath, ext)).OrderBy(Function(x) x).ToArray
    Which gives exactly the same time!

    But, for people interested in this topic, I've found that calling GetFiles once then filtering results by file extension is far better...especially when the number of extensions to look for is raising!

    Code:
    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!

IMN logo majestic logo threadwatch logo seochat tools logo