1

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

Thanks

Costa
  • 3,897
  • 13
  • 48
  • 81

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
  • 155,851
  • 29
  • 291
  • 343
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
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,395
  • 5
  • 49
  • 74