Bastien Vandamme
Bastien Vandamme

Reputation: 18485

Read files with EnumerateFiles and multiple filter (file extensions)

I need to load dynamically file from folder and sub folders.

I use this code to load my files:

    var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
        .Where(s => s.EndsWith(".txt") || s.EndsWith(".xml") || s.EndsWith(".log") || s.EndsWith(".csv"))
        .ToList();

It is slow but it is better than all other method I already tried.

Now I would like to change this line to have something dynamic so that I can filter my extensions

.Where(s => s.EndsWith(".txt") || s.EndsWith(".xml") || s.EndsWith(".log") || s.EndsWith(".csv"))

So, first step I replace my line with this:

filter.Split('|').ToList().ForEach(y =>
{
    files.AddRange(Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
        .Where(s => s.EndsWith(y))
        .ToList());
});

But then I can also remove the Where instruction and use the filter from EnumerateFiles(). Yes ok sure. But the problem is also by doing this i do multiple call to EnumerateFiles() and this is even more slower.

What can I do?

Is this better

var allFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);
var files = new List<string>();

filter.Split('|').ToList().ForEach(y =>
{
    files.AddRange(allFiles.Where(s => s.EndsWith(y)).ToList());
});

Upvotes: 2

Views: 591

Answers (1)

Rand Random
Rand Random

Reputation: 7440

I would do the following

var extensions = filter.Split('|').ToHashSet(StringComparer.OrdinalIgnoreCase);
var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
    .Where(s => extensions.Contains(Path.GetExtension(s)))
    .ToList();

Edit:

FYI your Is this better code is 100% the same as your previous code. IEnumerable is lazy and will only be exectued when used, so declaring var allFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories); outside of the loop makes no difference.

See here a demonstration: https://dotnetfiddle.net/UZj8lr

Upvotes: 3

Related Questions