我这有一个函数,功能是查找一个文件夹及其子文件夹中是否存在某个文件,也是要通过遍历各个子文件夹来实现的,你在对应的地方稍微改一下就实现你的功能了:
BOOL FindFileInFolder(CString strPathName, CString strFileName)
{
CFileFind finder;
CString strSearch;
BOOL bFind;
strFileName.MakeLower();
if (strPathName.Right(1) == "\\")
strSearch.Format("%s*.*", strPathName);
else
strSearch.Format("%s\\*.*", strPathName);
bFind = finder
.FindFile(strSearch);
if (!bFind)
{
finder.Close();
return FALSE;
}
while (bFind)
{
bFind = finder.FindNextFile();
strPathName = finder.GetFilePath(); // 取得文件全名
if (finder.IsDots()) continue; // 是点,忽略
if (finder.IsSystem()) continue; // 是系统文件,忽略
if (finder.IsDirectory()) // 是目录
{
if (FindFileInFolder(strPathName, strFileName)) // 递归调用,进入目录
{
finder.Close();
return TRUE;
}
}
else // 是文件
{
CString strFileFind = finder.GetFileName();
strFileFind.MakeLower();
if (strFileFind == strFileName)
{
finder.Close();
return TRUE;
}
}
}
finder.Close();
return FALSE;
}