Finding files question

This is a discussion on Finding files question within the Windows Programming forums, part of the Platform Specific Boards category; Hi i have string of files for days in month: 2008-8-1.txt, 2008-8-2.txt, 2008-8-3.txt,....., 2008-8-30, 2008-8-31. when i search thru these ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362

    Finding files question

    Hi i have string of files for days in month: 2008-8-1.txt, 2008-8-2.txt, 2008-8-3.txt,....., 2008-8-30, 2008-8-31. when i search thru these files, FindNextFile() finds 2008-8-30.txt file after 2008-8-3.txt. is there a way to change this file finding order of the function, so it finds 2008-8-4.txt after 2008-8-3.txt (correct order of days) ??

    Thanks for any help.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    I think you need to read all the files, then sort them using whatever criteria you decide you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,138
    also, you should use two digits for month and day. that will make the sorting work more to your liking.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    The computer does not see files the same way you see them in the 'Explorer' window for opening folders/files. Computers see everything in alphabetical order regardless of it being a file or folder (took me a while to figure that out when first using FindNextFile so just an FYI). Doing what Elkvis said should probably help.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Actually that's not true. Computers don't see everything in alphabetical order. opendir()+readdir()+closedir() (the UNIX equivalent of FindNextFile()) returns files in seemingly random order. I've never investigated, but I'd imagine files are stored in chronological order, more or less.

    What you say may be true for FindNextFile(), but it's never a good idea to be so sweepingly broad as "all computers are like this . . .", because it's probably not true.

    [edit] Personally, I'd just read the list of all the files you're interested in, as suggested, and then use a custom sorting function. I think considering digits to be very high values would work. (Because then a 0 would get sorted after a '-' or a '.'.)

    If you want to be more robust, you could rename the files as suggested so that they are all the same length. Or you could parse the string into tokens. If you split each name into digits and non-digits, you could convert the digits to numbers and then compare them as integers, and compare non-digits in ASCII order. I like the idea. [/edit]
    Last edited by dwks; 08-31-2008 at 03:59 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Good point, let me revise my wording then. The windows OS's that I have had experience on saw files in alphabetical order (XP and Vista).


  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I don't think that's true either. Have you ever executed dir in a directory with, say, 3000 files? It's more or less random order as well. (At least it was for me on Windows 98.)

    I'd be strongly inclined to think that FindNextFile() just sorts the files for you. It's possible that NT-based Windowses are different, I guess, but I wouldn't think so.

    Don't presume to know how Windows sees the files. Just state how FindNextFile() sees the files, which may or may not be different. If you did that, no one could argue with you.

    [edit] Oh, and for what it's worth:
    The order in which the search returns the files, such as alphabetical order, is not guaranteed, and is dependent on the file system. If the data must be sorted, the application must do the ordering after obtaining all the results.

    The order in which this function returns the file names is dependent on the file system type. With the NTFS file system and CDFS file systems, the names are usually returned in alphabetical order. With FAT file systems, the names are usually returned in the order the files were written to the disk, which may or may not be in alphabetical order. However, as stated previously, these behaviors are not guaranteed.
    From the MSDN page for FindNextFile. http://msdn.microsoft.com/en-ca/library/aa364428.aspx

    (The disk I was talking about was, of course, a FAT disk. I'd imagine your are NTFS. So there you have it.) [/edit]
    Last edited by dwks; 08-31-2008 at 04:05 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Revision no. 2

    When I used FindNextFile() it appeared to be in alphabetical order when I used it to search through my files.


  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by scwizzo View Post
    Revision no. 2

    When I used FindNextFile() it appeared to be in alphabetical order when I used it to search through my files.

    Yes, if the filesystem is NTFS (which I thoroughly recommend over the FAT alternative), then the files are (I'm pretty sure - there may still be situations when they are out of order [not at all sure what happens if new files are added DURING a pass over it with FindNextFile - this may be done out of order, but it may also just skip files added after you got them to a particular order]) guaranteed to be in alphabetical order.

    But the FindNextFile in itself just returns the files in the order they are stored in the filesystem - which may be in the order they were created, in alphabetical order or in order of hash-value of the filename for all you know.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    As far as I know, entries are returned in the order they're stored in the directory table (just like you would read lines from a text file say).

    So initially at least, it would seem to be date ordered (oldest first).

    But if you then delete some files and create new ones, I imagine those old directory entries would be re-used. For very old directories with lots of additions and deletions, the whole lot would seem pretty random.

    But that was for FAT file system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    As far as I know, entries are returned in the order they're stored in the directory table (just like you would read lines from a text file say).

    So initially at least, it would seem to be date ordered (oldest first).

    But if you then delete some files and create new ones, I imagine those old directory entries would be re-used. For very old directories with lots of additions and deletions, the whole lot would seem pretty random.

    But that was for FAT file system.
    Correct - the order they are stored in the directory table is the order they come out. Since the order in the directory table depends on the structure the filesystem uses, it can not be guaranteed in any way (there is even a "File System Develepoment Kit" from Microsoft, although it is fairly hard to get your hands on [and a good half-dozen or dozen different filesystems for Linux]).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    try YYYYMMDD.txt as the file name,

    then for a simple qsort, atoi the filename (which stops at the first non digit character)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by novacain View Post
    try YYYYMMDD.txt as the file name,

    then for a simple qsort, atoi the filename (which stops at the first non digit character)
    Or simply sort on strings - unless it's really huge number of files (like hundreds of thousands - which would mean several hundreds of years worth of files), the difference between comparing strings and comparing integers is going to be minimal. And it makes it easier to write the code. qsort compare function set up for strcmp() is also real trivial: Just make a wrapper function to pass the pointers to strings to strcmp, and return the value - job done.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. Finding files in a folder
    By ryeguy in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 01:00 AM
  3. a question files
    By amin1982 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 06:10 AM
  4. Multi - File Program not finding files
    By johnh444 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2004, 01:48 AM
  5. Finding out how many files are in a directory
    By mikie7boy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21