您的位置:首页 > 编程语言 > C语言/C++

c++ 递归查找指定类型的文件。

2011-04-08 15:39 295 查看
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();
TRACE(_T("%s/n"), (LPCTSTR)str);
Recurse(str);
}
else
{
CString strFilePath = finder.GetFilePath();
int flag = strFilePath.ReverseFind(_T('.'));
if(-1 != flag)
{
if(0 == strFilePath.Mid(flag).Compare(_T(".txt")))
{
DeleteFile(strFilePath);
}
}
}
}

finder.Close();
}

void PrintDirs()
{
Recurse(_T("F://"));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: