Skip to main content

Efficiently enumerate directory trees in C#.

var sourceFiles = Directory.EnumerateFiles(MTARootFolder, @"*.mta", SearchOption.AllDirectories).Where(f => !f.Contains(@"~snapshot")); // don't grab files in the hidden folder name '~snapshot'

foreach (var sourceFile in sourceFiles) {
    PatientSourceFileInfo = null;
    PatientSourceFileInfo = new FileInfo(sourceFile);

    List<string> pdfContent = new List<string>();
    SkipFile = false; // reset skip file flag

    using (var reader = new StreamReader(sourceFile)) {
        string line;

        while ((line = reader.ReadLine()) != null) {
            if (line.Contains(@"REPORT'S AUDIT TRAIL")) {
                System.Console.ForegroundColor = ConsoleColor.White;
                System.Console.WriteLine("Report Audit Trail file-type found... skipping.");
                System.Console.ResetColor();
                pdfContent.Clear();

                SkipFile = true;
                break;
            }
            pdfContent.Add(line);
        }
    }
}