CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    22

    FindNextFile() Looping and Backwards ?

    Hi guys, I'm using FindFirstFile() and FindNextFile() to go through all the files in a directory, I want to be able to go backwards and also loop at the beginning and end, like this:

    001.txt
    002.txt
    003.txt
    004.txt

    ...if I am at 003.txt and select back command it should go to 002.txt, if I am at 001.txt and select back command it should go to 004.txt.

    For forward looping I can start the process over again with FindFirstFile() but I am not sure if that is smart.

    I know that I can store all the filenames in an array but some directories may have 10k file counts so maybe that's not a good idea ?

    Thank you.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: FindNextFile() Looping and Backwards ?

    Why? What are you trying to accomplish?

    In the grand scheme of things, a 10K array isn't that big. If the average file name were 15 characters long, that's only 150K plus a bit of overhead.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: FindNextFile() Looping and Backwards ?

    I want to be able to go backwards and also loop at the beginning and end, like this:
    As far as I am aware there is no win32 API that provides the ability to start at the end of a directory and go backwards through the files. Also note that using FindFirstFile() and FIndNextFile() says nothing about the order in which the names of the files will be retrieved.

    If you want to be able to process/display the file names in a particular order or to traverse the file names in a particular way (eg backwards) then you will need to build a container (eg vector) with the file names, sort the container as required and then iterate through the container which can be forwards or backwards.

    As GCDEF says in post #2, a vector of 10k elements isn't that big and is easily handled.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 2011
    Posts
    22

    Re: FindNextFile() Looping and Backwards ?

    You guys are right, it's better to go with a filename array. It was pretty easy to implement and it's working great.

    Thanks.

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

Featured