C ++ MFC를 사용한 재귀 파일 검색?
-
06-09-2019 - |
문제
C ++ 및 MFC를 사용하여 파일을 재귀 적으로 검색하는 가장 깨끗한 방법은 무엇입니까?
편집 :이 솔루션 중 하나는 한 번의 패스를 통해 여러 필터를 사용할 수있는 기능을 제공합니까? cfilefind를 사용하면 *를 필터링 한 다음 다른 파일 유형으로 더 필터링하기 위해 사용자 정의 코드를 작성할 수 있다고 생각합니다. 내장 된 여러 필터 (예 : *.exe, *. dll)가 제공됩니까?
edit2 : 방금 이전 편집을 유효하게 만드는 것이 내가 만들고 있다는 명백한 가정을 깨달았습니다. cfilefind로 재귀 검색을하려는 경우 *를 사용해야합니다. * 와일드 카드가 일치하지 않고 재귀가 발생하지 않기 때문에 내 와일드 카드로서 사용해야합니다. 따라서 다른 파일 텍스트에서 필터링하면 별도로 처리해야합니다.
해결책
사용 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();
}
다른 팁
사용 부스트의 파일 시스템 구현!
재귀 예는 파일 시스템 홈페이지에도 있습니다.
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;
}
나는 그것이 당신의 질문이 아니라는 것을 알고 있지만, 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();
}
}
확인하십시오 recls 도서관 - recursive ls - 유닉스와 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 또는 그 하위 디렉터에서 ABC로 시작하는 모든 .doc 파일 및 모든 .xls 파일을 찾을 수 있습니다.
나는 이것을 편집하지 않았지만, 그것은 마크에 매우 가깝습니다.
CString strNextFileName , strSaveLog= "C:\\mydir";
Find.FindFile(strSaveLog);
BOOL l = Find.FindNextFile();
if(!l)
MessageBox("");
strNextFileName = Find.GetFileName();
작동하지 않습니다. find.findnextfile () false 반환 False 파일도 같은 디렉토리에 존재합니다.