Simple recursive seach question.

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)
{
      StartFile(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();
            GetDlgItem(IDC_CURRENT)->SetWindowText(tmp);

        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();
             StartFile(str_Path);
             WriteFileDir(str_Path);
             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?
cheapstrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
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.
Pseudo-code:

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

for each directory in vector
{
    FindFiles(directory)
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cheapstrAuthor Commented:
Thanks, I ended up using CArray, since it's simpler to use, but the idea worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.