3

使用 C++ 和 MFC 递归搜索文件的最简洁方法是什么?

编辑:这些解决方案中的任何一个都提供通过一次通过使用多个过滤器的能力吗?我想用 CFileFind 我可以过滤 *.* 然后编写自定义代码以进一步过滤到不同的文件类型。有没有提供内置的多个过滤器(即*.exe、*.dll)?

EDIT2:刚刚意识到我所做的一个明显假设使我之前的 EDIT 无效。如果我尝试使用 CFileFind 进行递归搜索,我必须使用 *.* 作为我的通配符,否则子目录将不会匹配并且不会发生递归。因此,无论如何都必须单独处理对不同文件扩展名的过滤。

4

5 回答 5

14

使用CFileFind.

看看 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();
}
于 2009-05-27T17:16:21.753 回答
4

使用Boost 的文件系统实现!

递归示例甚至在文件系统主页上:

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;
}
于 2009-05-27T17:14:19.183 回答
2

我知道这不是你的问题,但也很容易通过使用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();
  }
}
于 2009-05-27T17:27:33.683 回答
2

查看recls库 - 代表recursive ls - 这是一个适用于 UNIX 和 Windows 的递归搜索库。它是一个 C 库,可以适应不同的语言,包括 C++。从内存中,您可以使用如下内容:

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());
}

它会在 C:\mydir 或其任何子目录中找到所有 .doc 文件和所有以 abc 开头的 .xls 文件。

我还没有编译这个,但它应该非常接近标记。

于 2009-05-27T23:53:12.750 回答
-1
CString strNextFileName , strSaveLog= "C:\\mydir";
Find.FindFile(strSaveLog);
BOOL l = Find.FindNextFile();
if(!l)
    MessageBox("");
strNextFileName = Find.GetFileName();

它不工作。即使文件存在于同一目录中,Find.FindNextFile() 也会返回 false ``

于 2015-04-10T06:00:13.080 回答