Get the list of files with multiple extensions filter

S-Soft 666 Reputation points
2022-12-05T04:56:49.007+00:00

Hello,
Using this:

Dim MySourceFiles As IEnumerable(Of String) = Nothing

MySourceFiles = Directory.EnumerateFiles(inDir, "*.ext1", SearchOption.TopDirectoryOnly)

But I need few more extensions to list and to be added to MySourceFiles, so another:

MySourceFiles += Directory.EnumerateFiles(inDir, "*.ext2", SearchOption.TopDirectoryOnly)

Just can't use +=
So how to list multiple file extensions and add them all to the MySourceFiles?
Thanks in advance :)

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,666 Reputation points
    2022-12-05T05:21:00.21+00:00

    You may use the Union. Refer the following sample.

    var result = Directory.EnumerateFiles("D:\\Temp\\Files", "*.jpg")  
        .Union(Directory.EnumerateFiles("D:\\Temp\\Files", "*.png"))  
        .Union(Directory.EnumerateFiles("D:\\Temp\\Files", "*.mp4"))  
        .ToList();  
    

    To understand more about Union Method, read the below article

    https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.union?view=net-7.0

    Hope this helps


2 additional answers

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-12-05T16:13:10.673+00:00

    Hi
    Late to the party, but here is my example (gleaned/modified from old google search sample), If time is critical then this example may help.

    I incorporated @Sreeju Nair example to test differences. (all commented out)
    The results seem to show this example to take about 60% of the time from first example. (for large data sets is significant)

    Here is code for bot (need to rearrange stopwatch calls to try both. I just used my Visual studio Projects folder for testing purposes. File types can be added to 'filter' list as needed. I put results into a List(Of String) but could be anything.

        Dim sw As New Stopwatch  
        Dim path As String = "C:\Users\lesha\Documents\Projects"  
        Dim filter As String = "*.vb,*.resx"  
        Dim res As New List(Of String)  
        Dim counter As Integer = 0  
      
        'Do  
        '  Dim result As List(Of String) = Directory.EnumerateFiles(path, "*.vb", SearchOption.AllDirectories).Union(Directory.EnumerateFiles(path, "*.resx", SearchOption.AllDirectories)).ToList()  
        '  res.AddRange(result)  
        '  ' approx 625ms per cycle  
        '  counter += 1  
        'Loop Until counter = 20  
        'Dim t As Long = sw.ElapsedMilliseconds  
        'eg 12480ms for the 20 cycles  
      
        ' agreagated: average about 625ms per cycle on my system with 57560 results from 274000 total files  
      
        ' about 12693ms for all cycles  
        'counter = 0  
        'sw.Restart()  
        sw.Start()  
        Do  
          res.AddRange(filter.Split(","c).AsParallel().SelectMany(Function(x) Directory.EnumerateFiles(path, x, SearchOption.AllDirectories)).OrderBy(Function(x) x).ToArray())  
          'ex. 377ms per cycle  
      
          counter += 1  
        Loop Until counter = 20  
        Dim t As Long = sw.ElapsedMilliseconds  
        'ex. 7530ms for the 20 cycles  
      
        ' agreagated: average about 377ms per cycle on my system with 57560 results from 274000 total files  
        Stop  
    
    0 comments No comments

  2. Dewayne Basnett 1,381 Reputation points
    2022-12-05T16:18:50.027+00:00

    Depending on the number of files in the folder v how many filtered you MIGHT be better off reading all of the names into a list and filtering that.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.