Recursive file search using C++ MFC?

后端 未结 5 1153
失恋的感觉
失恋的感觉 2020-12-06 14:45

What is the cleanest way to recursively search for files using C++ and MFC?

EDIT: Do any of these solutions offer the ability to use multiple filters through one pas

相关标签:
5条回答
  • 2020-12-06 15:16

    I know it is not your question, but it is also easy to to without recursion by using a CStringArray

    void FindFiles(CString srcFolder)
    {   
      CStringArray dirs;
      dirs.Add(srcFolder + "\\*.*");
    
      while(dirs.GetSize() > 0) {
         CString dir = dirs.GetAt(0);
         dirs.RemoveAt(0);
    
         CFileFind ff;
         BOOL good = ff.FindFile(dir);
    
         while(good) {
            good = ff.FindNextFile();
            if(!ff.IsDots()) {
              if(!ff.IsDirectory()) {
                 //process file
              } else {
                 //new directory (and not . or ..)
                 dirs.InsertAt(0,nd + "\\*.*");
              }
            }
         }
         ff.Close();
      }
    }
    
    0 讨论(0)
  • 2020-12-06 15:17

    Use Boost's Filesystem implementation!

    The recursive example is even on the filesystem homepage:

    bool find_file( const path & dir_path,         // in this directory,
                    const std::string & file_name, // search for this name,
                    path & path_found )            // placing path here if found
    {
      if ( !exists( dir_path ) ) return false;
      directory_iterator end_itr; // default construction yields past-the-end
      for ( directory_iterator itr( dir_path );
            itr != end_itr;
            ++itr )
      {
        if ( is_directory(itr->status()) )
        {
          if ( find_file( itr->path(), file_name, path_found ) ) return true;
        }
        else if ( itr->leaf() == file_name ) // see below
        {
          path_found = itr->path();
          return true;
        }
      }
      return false;
    }
    
    0 讨论(0)
  • 2020-12-06 15:33
    CString strNextFileName , strSaveLog= "C:\\mydir";
    Find.FindFile(strSaveLog);
    BOOL l = Find.FindNextFile();
    if(!l)
        MessageBox("");
    strNextFileName = Find.GetFileName();
    

    Its not working. Find.FindNextFile() returning false even the files are present in the same directory``

    0 讨论(0)
  • 2020-12-06 15:35

    Using CFileFind.

    Take a look at this example from MSDN:

    void Recurse(LPCTSTR pstr)
    {
       CFileFind finder;
    
       // build a string with wildcards
       CString strWildcard(pstr);
       strWildcard += _T("\\*.*");
    
       // start working for files
       BOOL bWorking = finder.FindFile(strWildcard);
    
       while (bWorking)
       {
          bWorking = finder.FindNextFile();
    
          // skip . and .. files; otherwise, we'd
          // recur infinitely!
    
          if (finder.IsDots())
             continue;
    
          // if it's a directory, recursively search it
    
          if (finder.IsDirectory())
          {
             CString str = finder.GetFilePath();
             cout << (LPCTSTR) str << endl;
             Recurse(str);
          }
       }
    
       finder.Close();
    }
    
    0 讨论(0)
  • 2020-12-06 15:36

    Check out the recls library - stands for recursive ls - which is a recursive search library that works on UNIX and Windows. It's a C library with adaptations to different language, including C++. From memory, you can use it something like the following:

    using recls::search_sequence;
    
    
    CString dir = "C:\\mydir";
    CString patterns = "*.doc;abc*.xls";
    CStringArray paths;
    search_sequence files(dir, patterns, recls::RECURSIVE);
    
    for(search_sequence::const_iterator b = files.begin(); b != files.end(); b++) {
        paths.Add((*b).c_str());
    }
    

    It'll find all .doc files, and all .xls files beginning with abc in C:\mydir or any of its subdirectories.

    I haven't compiled this, but it should be pretty close to the mark.

    0 讨论(0)
提交回复
热议问题