• 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

FileScanner

FileScanner scans the tree structure of a filesystem for files that match its configured criteria. It uses concurrent threads.

IFileSystem fs = new MemoryFileSystem();
fs.CreateDirectory("myfile.zip/folder");
fs.CreateFile("myfile.zip/folder/somefile.txt");

FileScanner filescanner = new FileScanner(fs);

FileScanner needs to be populated with at least one filter, such as wildcard pattern, .AddWildcard(string).

filescanner.AddWildcard("*.zip");

Or regular expression.

filescanner.AddRegex(path: "", pattern: new Regex(@".*\.zip"));

Or glob pattern.

filescanner.AddGlobPattern("**.zip/**.txt");

The initial start path is extracted from the pattern.

filescanner.AddGlobPattern("myfile.zip/**.txt");

Search is started when IEnumerator<IEntry> is enumerated from the scanner.

foreach (IEntry entry in filescanner)
{
    Console.WriteLine(entry.Path);
}

Exceptions that occur at real-time can be captured into concurrent collection.

// Collect errors
filescanner.errors = new ConcurrentBag<Exception>();
// Run scan
IEntry[] entries = filescanner.ToArray();
// View errors
foreach (Exception e in filescanner.errors) Console.WriteLine(e);

The property .ReturnDirectories determines whether scanner returns directories.

filescanner.ReturnDirectories = true;

The property .SetDirectoryEvaluator(Func<IEntry, bool> func) sets a criteria that determines whether scanner enters a directory.

filescanner.SetDirectoryEvaluator(e => e.Name != "tmp");
Back to top Copyright © 2015-2020 Toni Kalajainen