Results 1 to 5 of 5

Thread: big in GetFiles()

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    big in GetFiles()

    Sorry, i meant "bug" in GetFiles()..but you probably know that



    I wish to pull "*.dft"


    Unfortunately, there seems to be a bug which causes this to actually be "*.dft*".

    I can't have it pull "*.dft*" because i have .dft files as well as .dft_backup files which are automatically generated. This is extremely inconvenient.

    It's inconvenient because i call Array.Sort after getting the files..and it throws an exception saying it cannot compare the .dft file with the .dft_backup file. (If this did not cause an exception, my code would end up just deleting the .dft_backup file, which would be ok with me.)


    Here is the basic snippet:

    Code:
    DirectoryInfo loadDirPath = new DirectoryInfo(this.basePath + "\\" + monthYearDir + "\\" + dayDir + "\\" + loadDir);
    
    FileInfo[] allDFTFilesInLoadDir = loadDirPath.GetFiles("*.dft");
    string[] test = Directory.GetFiles(loadDirPath.ToString(), "*.dft");
    Both of them show 2 files when there is really only one .dft file.



    Anyone know how I could get around this?...without having to split on '.' and counting the characters of element[1]. Thanks
    Last edited by icawn; Jun 24th, 2009 at 01:32 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: big in GetFiles()

    It's not a bug. It's by design. This is another example of why you should read the documentation:
    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.


    The following list shows the behavior of different lengths for the searchPattern parameter:

    "*.abc" returns files having an extension of .abc, .abcd, .abcde, .abcdef, and so on.

    "*.abcd" returns only files having an extension of .abcd.

    "*.abcde" returns only files having an extension of .abcde.

    "*.abcdef" returns only files having an extension of .abcdef.
    There's no way to make it behave differently, so you'll have to remove the unwanted file names after the fact. If you're targeting .NET 3.5 then this is easy using LINQ:
    Code:
    FileInfo[] allDFTFilesInLoadDir = loadDirPath.GetFiles("*.dft").Where(f => f.Extension == ".dft");
    string[] test = Directory.GetFiles(loadDirPath.ToString(), "*.dft").Where(s => Path.GetExtension(s) == ".dft");
    For earlier versions you'll have to use a loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    Re: bug in GetFiles()

    Thanks for helping. It works great. I really do try and "read the documentation" to learn, rather than run here first looking for answers, but i gave up after nearly an hour of searching. I guess i just wasn't using the right key words for searching.

    I will become familiar with the LINQ library because our computers do have .NET 3.5. I was playing around with loadDirPath.GetFiles("*.dft").OrderByDescending<> before this, but I could not get it to compile. I had not used LINQ before and was not understanding the IEnumerable "f => f" type syntax. It seems a heck of a lot better than Array.Sort Array.Reverse.

    I still don't completely understand the LINQ IEnumerable ~syntax though I haven't had the time to read and research that.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bug in GetFiles()

    Quote Originally Posted by icawn View Post
    I guess i just wasn't using the right key words for searching.
    That seems to be one of the main problems, i.e. people searching when they shouldn't be. You already know exactly what type and member you're using so you should go straight to the documentation for that type and member. That's as easy as clicking it in code and pressing F1, or you can use the Help -> Index menu in VS.
    Quote Originally Posted by icawn View Post
    I still don't completely understand the LINQ IEnumerable ~syntax though I haven't had the time to read and research that.
    You can pretty much think of a lambda expression as a foreach loop, e.g. the Where method returns a subsequence where a particular condition is true. In my code the lambda passed to Where:
    Code:
    s => Path.GetExtension(s) == ".dft"
    is basically say:

    for each file path s in the sequence, return the result of comparing that path's extension with the literal ".dtf". That lambda will evaluate to true or false for each file path s in the sequence and the Where method will filter out those that are false.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    Re: big in GetFiles()

    Thanks. I can see then how some Enumberables would need to use a side method/class, such as OrderBy and OrderByDescending, since they're not as straight forward as Where

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width