递归文件搜索使用C ++ MFC? [英] Recursive file search using C++ MFC?
问题描述
使用C ++和MFC递归搜索文件的最简洁的方法是什么?
What is the cleanest way to recursively search for files using C++ and MFC?
编辑:任何这些解决方案都能够通过一遍使用多个过滤器?我想用CFileFind我可以过滤*。*然后写自定义代码进一步过滤到不同的文件类型。是否有提供内置的多个过滤器(即* .exe,*。dll)?
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)?
EDIT2:刚刚实现了一个明显的假设, EDIT无效。如果我想用CFileFind进行递归搜索,我必须使用*。*作为我的通配符,因为否则子目录将不会匹配,并且不会发生递归。因此,不同文件范围的过滤都必须单独处理。
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.
推荐答案
使用 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();
}
这篇关于递归文件搜索使用C ++ MFC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!