Skip to content

Instantly share code, notes, and snippets.

@ArtemAvramenko
Last active May 26, 2023 20:05
Show Gist options
  • Save ArtemAvramenko/d1c1b71b8aa427a4305fb7920a5d197d to your computer and use it in GitHub Desktop.
Save ArtemAvramenko/d1c1b71b8aa427a4305fb7920a5d197d to your computer and use it in GitHub Desktop.
Get all files in all subfolders by regexp mask in C#
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