使用 C++ MFC 进行递归文件搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/916973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive file search using C++ MFC?
提问by jacobsee
What is the cleanest way to recursively search for files using C++ and MFC?
使用 C++ 和 MFC 递归搜索文件的最简洁方法是什么?
EDIT: Do any of these solutions offer the ability to use multiple filters through one pass? I guess with CFileFind I could filter on *.* and then write custom code to further filter into different file types. Does anything offer built-in multiple filters (ie. *.exe,*.dll)?
编辑:这些解决方案中的任何一个都提供通过一次通过使用多个过滤器的能力吗?我想使用 CFileFind 我可以过滤 *.* 然后编写自定义代码以进一步过滤到不同的文件类型。是否提供内置的多个过滤器(即 *.exe、*.dll)?
EDIT2: Just realized an obvious assumption that I was making that makes my previous EDIT invalid. If I am trying to do a recursive search with CFileFind, I have to use *.* as my wildcard because otherwise subdirectories won't be matched and no recursion will take place. So filtering on different file-extentions will have to be handled separately regardless.
EDIT2:刚刚意识到我所做的一个明显假设使我之前的编辑无效。如果我尝试使用 CFileFind 进行递归搜索,我必须使用 *.* 作为我的通配符,否则将无法匹配子目录并且不会发生递归。因此,无论如何都必须单独处理对不同文件扩展名的过滤。
回答by aJ.
Using CFileFind
.
使用CFileFind
.
Take a look at this examplefrom MSDN:
看看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();
}
回答by Kieveli
Use Boost's Filesystemimplementation!
使用Boost 的文件系统实现!
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;
}
回答by crashmstr
I know it is not your question, but it is also easy to to without recursion by using a CStringArray
我知道这不是你的问题,但也很容易通过使用 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();
}
}
回答by DannyT
Check out the reclslibrary - 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:
退房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());
}
It'll find all .doc files, and all .xls files beginning with abc in C:\mydir or any of its subdirectories.
它将在 C:\mydir 或其任何子目录中找到所有 .doc 文件和所有以 abc 开头的 .xls 文件。
I haven't compiled this, but it should be pretty close to the mark.
我还没有编译这个,但它应该非常接近标记。
回答by Chetan
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``
它不工作。Find.FindNextFile() 返回 false 即使文件存在于同一目录中``