0

How would I search all the files in directory and all its sub directories for a specific extension

Directory.GetFiles(path, ".txt", SearchOption.AllDirectories);

The code above returns no files

MarkP
  • 4,745
  • 3
  • 22
  • 18
user2282583
  • 41
  • 1
  • 1
  • 6

4 Answers4

9

you need to use wild card notation

Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);

in your case you're searching for files ".txt" name, instead you need tell to API to retrieve to you all files that has txt extension.

Tigran
  • 61,654
  • 8
  • 86
  • 123
7

Because you're searching literally for the file named .txt

Use a wildcard character like so: *.txt and it should pull up any .txt files.

See documentation: http://msdn.microsoft.com/en-us/library/ms143316.aspx

tnw
  • 13,521
  • 15
  • 70
  • 111
6

I believe its your search pattern or second parameter. should be "*.txt".

Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
2

The filter needs to be "*.txt":

Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
Jan
  • 2,168
  • 2
  • 19
  • 28