0

I have this code using GetFiles with search pattern:

Dim wordFiles As FileInfo()= New DirectoryInfo(source).GetFiles("*.doc")

The files in the source directory are:

  1. Test.doc
  2. Test1.docx
  3. test3.docxxx
  4. test2.doc_xxx

Why am I getting files which don't have the exact search pattern? I get:

  1. Test.doc
  2. Test1.docx ' not expected
  3. test2.doc_xxx ' not expected

I only expect 'Test.doc' as the output file.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
monikapatelIT
  • 977
  • 14
  • 26

2 Answers2

2

MSDN says this is expected behaviour:

When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern.

A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension.

For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

You could filter the files after you get them:

New DirectoryInfo(source).
    GetFiles("*.doc").
    Where(Function (t) t.Extension.Equals(".doc", StringComparison.OrdinalIgnoreCase)).
    ToArray()
Community
  • 1
  • 1
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

As MSDN states according to this link: DirectoryInfo.GetFiles Method (String, SearchOption)

When using the asterisk wildcard character in a searchPattern (for example, "*.doc"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while the other search pattern returns both files.

I took the liberty of trying to find a workaround for you, but it seems the only viable one would work on .NET 4.0 and above only: GetFiles with multiple and specific extentions c#

string[] patterns = new { ".xls", ".xlsx" };
return patterns.AsParallel().SelectMany(p => 
    Directory.EnumerateFiles(path, p, SearchOption.AllDirectories));
Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41