問題描述
使用 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)?
剛剛意識到我所做的一個明顯假設使我之前的 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:
Take a look at this example from 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 進行遞歸文件搜索?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!