ib3an
ib3an

Reputation: 241

searchPattern Logic on Directory.GetFiles Method

I would like to know what is the search pattern logic on Directory.GetFiles Method. I use asterisk wildcard on my search pattern. I do not understand what kind of logic to apply on searching if i put "*" in-front of char.

I got unexpected result if i put "*" in-front of char but it was correct if i put at behind of char.

Here is file list in folder, sample code and result.

enter image description here

Asterisk in-front of char

string _strSearchPattern = "*1";
foreach (string _strFolder in Directory.GetFiles(@"C:\Temp\FileList", _strSearchPattern))
Console.WriteLine("{0}", _strFolder);

Unexpected result. It should be 1. Why "b_Request" is come out but why not "b" is include?

enter image description here

Asterisk behind of char

string _strSearchPattern = "1*";
foreach (string _strFolder in Directory.GetFiles(@"C:\Temp\FileList", _strSearchPattern))
Console.WriteLine("{0}", _strFolder);

Here is expected result

enter image description here

Is it bug or i'm thinking too much?

Upvotes: 2

Views: 988

Answers (1)

Chasefornone
Chasefornone

Reputation: 757

That is sort of tricky but not a bug.

Asterisk (*) stands for zero or more characters in that position and question mark (?) stands for zero or one character in that position.

According to MSDN:

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

In you first case, a search path with "*1" will match any path ends with letter 1, path 1 and path b_Request (with the 8.3 file name format b_Requ~1) will be returned.

You can refer here for more about 8.3 filename.

Upvotes: 3

Related Questions