locked
System.IO.Directory.GetFiles RRS feed

  • Question

  • I have a function that gets all files in all directories, however System.IO.Directory.GetFiles(String, String, SearchOption) doesnt appear to be functioning correctly.

    In a folder I have the following files
    C:\test\test1\123.1bc
    C:\test\test2\123.1bc
    C:\test\test3\123.1bcyyy

    C:\test\test4\123.1bchhh

    C:\test\test5\123.1bcrrr
    C:\test\test6\123.1bceee

    System.IO.Directory.GetFiles("C:\test", "*.1bc", IO.SearchOption.AllDirectories)

    I would expect it to return only the 2 .1bc files, however it returns all 6 files.

    This doesnt appear to be correct behavior... Am i missing something or is this a bug?


    • Edited by Stimphy Friday, December 6, 2013 2:51 AM typo
    Friday, December 6, 2013 2:48 AM

Answers

  • Hi, It looks like that is the intended behaviour for *.ext searches see this quote from MSDN:

    When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having 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 having 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, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file*.txt" returns both files.

    • Marked as answer by Stimphy Friday, December 6, 2013 3:18 AM
    Friday, December 6, 2013 2:53 AM

All replies

  • Hi, It looks like that is the intended behaviour for *.ext searches see this quote from MSDN:

    When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having 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 having 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, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file*.txt" returns both files.

    • Marked as answer by Stimphy Friday, December 6, 2013 3:18 AM
    Friday, December 6, 2013 2:53 AM
  • So this is a feature not a bug...

    Guess I'll have to code around it, thanks for the quick response!

    Friday, December 6, 2013 3:17 AM