[英]Recursive file search using C++ MFC?
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.
我想使用 CFileFind 我可以过滤 *.* 然后编写自定义代码以进一步过滤到不同的文件类型。 Does anything offer built-in multiple filters (ie. *.exe,*.dll)?
是否提供内置的多个过滤器(即 *.exe、*.dll)?
EDIT2: Just realized an obvious assumption that I was making that makes my previous EDIT invalid. EDIT2:刚刚意识到我所做的一个明显的假设使我之前的编辑无效。 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.
如果我尝试使用 CFileFind 进行递归搜索,我必须使用 *.* 作为我的通配符,否则将无法匹配子目录并且不会发生递归。 So filtering on different file-extentions will have to be handled separately regardless.
因此,无论如何都必须单独处理对不同文件扩展名的过滤。
Using CFileFind
.使用
CFileFind
。
Take a look at this example from 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();
}
Use Boost's Filesystem implementation!使用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;
}
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();
}
}
Check out the recls library - stands for rec ursive ls - which is a recursive search library that works on UNIX and Windows.退房recls库-代表REC ursive LS -这是一个递归搜索库,UNIX和Windows的作品。 It's a C library with adaptations to different language, including C++.
它是一个适应不同语言的 C 库,包括 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.它将在 C:\\mydir 或其任何子目录中找到所有 .doc 文件和所有以 abc 开头的 .xls 文件。
I haven't compiled this, but it should be pretty close to the mark.我还没有编译这个,但它应该非常接近标记。
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 即使文件存在于同一目录中``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.