Filtering file list and removing unwanted extensions using Enumerable and
Lambda
I am using this code
private IEnumerable<String> FindAccessableFiles(string path, string
file_pattern, bool recurse)
{
IEnumerable<String> emptyList = new string[0];
if (File.Exists(path))
return new string[] { path };
if (!Directory.Exists(path))
return emptyList;
var top_directory = new DirectoryInfo(path);
// Enumerate the files just in the top directory.
var files = top_directory.EnumerateFiles(file_pattern);
var filesLength = files.Count();
var filesList = Enumerable
.Range(0, filesLength)
.Select(i =>
{
string filename = null;
try
{
var file = files.ElementAt(i);
filename = file.FullName;
}
catch (UnauthorizedAccessException)
{
}
catch (InvalidOperationException)
{
// ran out of entries
}
return filename;
})
.Where(i => null != i);
if (!recurse)
return filesList;
var dirs = top_directory.EnumerateDirectories("*");
var dirsLength = dirs.Count();
var dirsList = Enumerable
.Range(0, dirsLength)
.SelectMany(i =>
{
string dirname = null;
try
{
var dir = dirs.ElementAt(i);
dirname = dir.FullName;
return FindAccessableFiles(dirname, file_pattern,
recurse);
}
catch (UnauthorizedAccessException)
{
}
catch (InvalidOperationException)
{
// ran out of entries
}
return emptyList;
});
return Enumerable.Concat(filesList, dirsList);
}
I've been hitting some performance problems iterating over folder that
have 100k+ files in them - all images that I ignore when I enumerate over
them.
I'm trying to work out how to exclude them from the enumerated list so
they are never processed in the first place but can't work out how to do
it.
I have a List<String> of extension I want to exclude and do this in the
code using Contains.
Would I get a performance gain if I excluded them from FindAccessableFiles
in the first place and how would I do it ? MY initial attempt was the
throw an exception if the file extension was contained in the extensions
list but I'm sure this isn't the best way.
The purpose of FindAccessableFiles is to produce a list of files that
circumvented the problems of GetFiles() throwing an exception trying to
access a file that threw a permissions error.
No comments:
Post a Comment