Last active
May 26, 2023 20:05
-
-
Save ArtemAvramenko/d1c1b71b8aa427a4305fb7920a5d197d to your computer and use it in GitHub Desktop.
Get all files in all subfolders by regexp mask in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private const string BaseDir = @"C:\Users\username\Desktop\dir"; | |
private static readonly Regex _files = new( | |
@".*\.(xml)$", | |
RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
private static readonly Regex _ignoredDir = new( | |
"^(node_modules|bin|obj)$", | |
RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
private static readonly Regex _ignoredContent = new( | |
"^.{1,100}<auto-generated", | |
RegexOptions.Singleline | RegexOptions.Compiled); | |
private static IEnumerable<string> GetAllFiles(DirectoryInfo root) | |
{ | |
foreach (var fileInfo in root.GetFiles("*.*")) | |
{ | |
if (_files.IsMatch(fileInfo.Name)) | |
{ | |
yield return (fileInfo.FullName); | |
} | |
} | |
foreach (var dirInfo in root.GetDirectories()) | |
{ | |
if (!_ignoredDir.IsMatch(dirInfo.Name)) | |
{ | |
foreach (var fullName in GetAllFiles(dirInfo)) | |
{ | |
yield return fullName; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment