Simple recursive seach question.

Posted on 2006-03-25
Last Modified: 2013-11-20
I am trying to create an app that will create an index list of all files in a directory. So if I pick a parent directory it will create a list of all files in that directory and create a list in each 'child' folder for their contents. Here is my code.

void CSiteIndexDlg::FindFiles(CString str_path)
      stopped = false;
      CFileFind my_finder;
      BOOL b_working = my_finder.FindFile(str_path + "\\*.*");
    //str_Path = str_path;
      while(b_working && !stopped)
            b_working = my_finder.FindNextFile();
            str_file_found = my_finder.GetFileName();
            CString tmp = my_finder.GetFilePath();

        if(!my_finder.IsDots() && !my_finder.IsDirectory())
                               // Get size of file in bytes and covert to kb, mb, or gb.
            struct _stat  status;
            _stat(tmp, &status );
            double size = status.st_size;

            str_file_found = my_finder.GetFileName();
            WriteFile(str_file_found, ConvertSize(size));

            if (!my_finder.IsDots() && my_finder.IsDirectory())
                      str_Path = my_finder.GetFilePath();
             FindFiles(str_Path); // Call itself
The 'search' portion seems to work, but not the way I want it to. It seems to find all the files in the parent directory, until it reaches a folder, then it moves into that child folder and continues, assigning the remaining files in the parent directory to the child directory. For example if my structure looked like this:
***Contents of Parent Directory****                     *****Contents of folder1*******
(Parent Directory:)    HeadFolder                                        file 5
(file)                        file 1                                                 file 6                  
(file)                        file 2                                    
(folder)                    folder1    -------------->
(file)                        file 3
(file)                        file 4

What I end up with is a list that looks like:
 ****list for head folder*****                           ****list for child folder*****
(Parent Directory)     HeadFolder                               (Child Folder)    folder1
(file)                        file 1                                        (file)                 file 3
(file                         file 2                                        (file)                 file 4
                                                                              (file)                file 5
                                                                              (file)                file 6

How can I make this search list ALL files in the parent directory first, then go into each child directory and list ALL it it's contents?
Question by:cheapstr
    LVL 48

    Accepted Solution

    Order of files reurned by CFileFind is not defined. You need to write file name together with path to output, and then sort output by path, or keep all directories in some vector and recursively call them in the end.

    vector<CString> directories;
    while(...)     // handle all files and directories
        if ( file )
             handle file
             add directory to vector

    for each directory in vector

    Author Comment

    Thanks, I ended up using CArray, since it's simpler to use, but the idea worked.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now