3

C ++とMFCを使用してファイルを再帰的に検索する最もクリーンな方法は何ですか?

編集:これらのソリューションのいずれかが、1つのパスで複数のフィルターを使用する機能を提供しますか?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

UNIXおよびWindowsで動作する再帰検索ライブラリであるreclsライブラリ(再帰ls)を確認してください。これは、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());
}

すべての.docファイル、およびabcで始まるすべての.xlsファイルがC:\mydirまたはそのサブディレクトリにあります。

私はこれを編集していませんが、マークにかなり近いはずです。

于 2009-05-27T23:53:12.750 に答える
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 に答える
-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 に答える