none
searchPattern Logic on Directory.GetFiles Method(C#) RRS feed

  • Question

  • 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.

    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?

    Asterisk behind of char

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

    Result

    Is it bug or i'm thinking too much?

    Regards,


    Make Simple & Easy

    Sunday, August 21, 2016 8:04 AM

Answers

  • Hello,

     Try reading the information provided by this link;

    https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx

     Make sure you read the Notes section as the return objects may varying depending

    on the criteria supplied for specified search pattern as to the OS naming conventions.

     Hope this helps :)

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 11:57 AM
  • In order to check the alternative short filenames, try executing this command line from Command Prompt window:

        dir/X C:\Temp\FileList\

    You will probably see that “b_Request” has a short name that includes “1”, such as “B_REQU~1”, which explains why “*1” matches it.

    By the way, these special short names can be obtained programmatically too.

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 4:40 PM
  • GetFiles() is one of those cases where the actuall work is left to the OS. So whatever it decides to be valid for Filters applies, down to it's wildcards. As a side effect the behavior might be dependant on the OS and compatibility mode your programm is running under too.
    It is the kind of function if having a canonical result (whatever Windows itself would say) is more important then a perfect result.

    Most likely the code is stubling over the old 8.3 filename, wich are still maintained for compatibility reasons.

    Note that both searchpatterns are not the same.
    The first finds you every file ending on a 1 (extension excluded). The second every file beginning with a 1. Both are not that helpfull. Among the first filters you usually do is for the file extension, if nothing else because it keeps the rest short.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 5:04 PM

All replies

  • Hello,

     Try reading the information provided by this link;

    https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx

     Make sure you read the Notes section as the return objects may varying depending

    on the criteria supplied for specified search pattern as to the OS naming conventions.

     Hope this helps :)

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 11:57 AM
  • In order to check the alternative short filenames, try executing this command line from Command Prompt window:

        dir/X C:\Temp\FileList\

    You will probably see that “b_Request” has a short name that includes “1”, such as “B_REQU~1”, which explains why “*1” matches it.

    By the way, these special short names can be obtained programmatically too.

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 4:40 PM
  • GetFiles() is one of those cases where the actuall work is left to the OS. So whatever it decides to be valid for Filters applies, down to it's wildcards. As a side effect the behavior might be dependant on the OS and compatibility mode your programm is running under too.
    It is the kind of function if having a canonical result (whatever Windows itself would say) is more important then a perfect result.

    Most likely the code is stubling over the old 8.3 filename, wich are still maintained for compatibility reasons.

    Note that both searchpatterns are not the same.
    The first finds you every file ending on a 1 (extension excluded). The second every file beginning with a 1. Both are not that helpfull. Among the first filters you usually do is for the file extension, if nothing else because it keeps the rest short.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    • Marked as answer by Yukonn Monday, August 22, 2016 3:56 AM
    Sunday, August 21, 2016 5:04 PM
  • Hi All,

              Thanks for your reply and point to right direction. All of your answer is cleared my doubt.

    Regards,


    Make Simple & Easy

    Monday, August 22, 2016 3:56 AM