File Scanner
FileScanner is a tool that scans directories recursively searching for files. It uses concurrent threads.
FileScanner fileScanner = new FileScanner();
It needs to be populated with filters, such as wildcard pattern with .AddFilename(string).
fileScanner.AddFilename("*.zip");
Or regular expressions.
fileScanner.AddRegex(".", new Regex(@".*\.zip"));
Or glob pattern.
fileScanner.AddGlobPattern(".", "**.zip");
Search is started when IEnumerator<string> is taken from the scanner.
foreach (string filepath in fileScanner)
{
Console.WriteLine(filepath);
}
Exceptions that occur at real-time can be captured into concurrent collection.
fileScanner.errors = new ConcurrentBag<Exception>();
Glob Pattern
Glob pattern is a file matching pattern, an alternatite to wildcard pattern.
Glob pattern uses the following notation:
- "*" matches to string of characters within the same directory.
- "?" matches to any character except directory separator.
- "**" matches to any characters, including directory separators.
Regex globPattern = new GlobPattern("**/*.dll/**.resources");
Test matching. "somefile.zip/" is excluded because of the separator in the pattern.
string[] files = new[]
{
"somefile.zip",
"somefile.zip/somefile.ext",
"somefile.zip/somefile.ext/someresource",
"somefile.zip/somelib.dll",
"somefile.zip/somelib.dll/someresource.resources",
"somelib.dll",
"somelib.dll/someresource.resources",
};
foreach (string filename in files.Where(fn => globPattern.IsMatch(fn)))
Console.WriteLine(filename);
The result.
somefile.zip/somelib.dll/someresource.resources