• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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)
      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?
1 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
cheapstrAuthor Commented:
Thanks, I ended up using CArray, since it's simpler to use, but the idea worked.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now