菜鸟系列:与CFileFind有关!
我用CFileFind()搜索文件,设CFileFind finder;请问如何用finder.IsDots()和
finder.IsSystem()控制搜索深度,当这两个函数为真是,如何继续搜索?请给出实现细节,感激不尽!
菜鸟系列,请多指教!
问题点数:100、回复次数:2Top
1 楼strip(阿飞 - Mozilla●CSDN●痛)回复于 2002-03-01 15:48:42 得分 70
IsDots()和IsDirectory()吧?
msdn里面有个非常简单的sample:
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();
}
Top
2 楼ccnuxjg()回复于 2002-03-01 15:49:05 得分 30
给你个例子:
BOOL CCutfoldDlg::DelDirectory(CString DirName)
{
CFileFind tempFind;
char tempFileFind[200];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory())
{
char tempDir[200];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DelDirectory(tempDir);
}
else
{
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(DirName))
{
::MessageBox(0,"删除目录失败!","警告信息",MB_OK);
return FALSE;
}
return TRUE;
}Top




