Link to home
Start Free TrialLog in
Avatar of cheapstr
cheapstr

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cheapstr
cheapstr

ASKER

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