1

How to find all files matches a regex pattern in a folder?

Thanks

Costa
  • 3,795
  • 11
  • 44
  • 78

2 Answers2

14

The GetFiles method allows you to specify a wildcard pattern but not really a regex. Another possibility is to simply loop through the files and validate their name against a regex.

IEnumerable<string> files = Directory
    .EnumerateFiles(@"c:\somePath")
    .Where(name => Regex.IsMatch(name, "SOME REGEX"));
Fredrik Mörk
  • 151,624
  • 28
  • 285
  • 338
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
0

Regex matching of filesystem is not supported you will have to iterate through each of the files in the directory and check them individually

Nissim
  • 6,106
  • 5
  • 47
  • 72