• Lexical
Show / Hide Table of Contents
  • Lexical.FileSystem
    • Introduction
    • Abstractions
      • IFileSystem
        • IFileSystem
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpen
      • IEvent
      • IEntry
      • IOption
      • IToken
    • FileSystem
    • VirtualFileSystem
    • MemoryFileSystem
    • EmbeddedFileSystem
    • HttpFileSystem
    • Decoration
    • IFileProvider
    • Utility
      • DisposeList
      • FileScanner
      • VisitTree
      • File Operation
  • Lexical.FileProvider
    • Introduction
    • Package
    • Package.Abstractions
    • Root
    • Zip
    • Dll
    • SharpCompress
    • SharpZipLib
    • FileScanner
    • Utils
  • Lexical.Localization
    • Introduction
    • Tutorial
    • Asset
      • IAsset
      • IStringAsset
    • Line
      • ILine
      • ILineFactory
      • ILineRoot
      • ILineFormat
      • ILineLogger
      • LineComparer
    • File
      • ILineReader
      • ILineWriter
      • Ini
      • Json
      • Xml
      • Resx
      • Resources
    • Miscellaneous
      • Plurality
      • ICulturePolicy
      • IStringFormat
      • Dependency Injection
    • Practices
      • Class Library
      • Class Library DI
      • Class Library DI opt.
  • Lexical.Utilities
    • Introduction
    • UnicodeString
    • FileScanner
    • Permutation
    • Tuples
    • StructList

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
Back to top Copyright © 2015-2020 Toni Kalajainen