1

Is there a more efficient way to filter file names using Directory.GetFiles and 'StartsWith', 'Contains' and 'EndsWith' with rather than the way I am currently doing it?

        _files = Directory.GetFiles(_path);

        if (!String.IsNullOrEmpty(_startsWith))
        {
            _files = _files.Where(x => x.StartsWith(_startsWith)).ToArray();
        }

        if (!String.IsNullOrEmpty(_contains))
        {
            _files = _files.Where(x => x.Contains(_contains)).ToArray();
        }

        if (!String.IsNullOrEmpty(_endsWith))
        {
            _files = _files.Where(x => x.EndsWith(_endsWith)).ToArray();
        }
Paul Bidwell
  • 77
  • 1
  • 7
  • 2
    The very slowest part will always be `Directory.GetFiles`, which is _almost_ unavoidable. Searching for the strings is comparatively negligible, that's not what you want to improve, is it? – Grant Thomas Mar 05 '13 at 12:02
  • 1
    There is an overload of GetFiles() where you can provide a searchpattern, have you tried that? – Jobo Mar 05 '13 at 12:04

3 Answers3

2

You should switch to Directory.EnumerateFiles() cause it is lazy and doesn't need to built up the complete list in the first place.

Oliver
  • 43,366
  • 8
  • 94
  • 151
0

I think following overloads will help you:

Directory.GetFiles(strPath, "*" + strFilter, SearchOption.TopDirectoryOnly); // to get behaviour of EndsWith

Directory.GetFiles(strPath, "*" + strFilter + "*", SearchOption.TopDirectoryOnly); // to get behaviour of Contains

Directory.GetFiles(strPath, strFilter + "*", SearchOption.TopDirectoryOnly); // to get behaviour of StartsWith
mihirj
  • 1,199
  • 9
  • 15
0

If you are trying to cram everything into a single line of Linq, you can do something like the following:

_files = _files.Where(_ => !String.IsNullOrEmpty(_startsWith)).Where(x => x.StartsWith(_startsWith)).ToArray();

However, as mentioned above, the Directory.GetFiles will almost certainly be the slowest part of the code here and you are better off sticking with something which is easier to read.

Sean Reid
  • 911
  • 9
  • 19