CFileFind遍历目录 查找 删除等

void BrowseDir(CString strDir)
{
CFileFind ff;
CString szDir = strDir;


if(szDir.Right(1) != "\\")
szDir += "\\";


szDir += "*.*";


BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
BrowseDir(ff.GetFilePath());
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
//显示当前访问的文件
CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_FILE);
CString str;
str.Format("当前访问的文件:%s",ff.GetFilePath());
p->SetWindowText(str);
Sleep(500);
}
}
ff.Close();//关闭
}


void SearchFile(CString strDir,CString strFile)
{
CFileFind ff;
CString szDir = strDir;


if(szDir.Right(1) != "\\")
szDir += "\\";


szDir += "*.*";


BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.GetFileName()==strFile)
{
//找到了,加入列表框中
m_ctrlFilesList.AddString(ff.GetFilePath());
}
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
SearchFile(ff.GetFilePath(),strFile);
}
}
ff.Close();//关闭
}


void RecursiveDelete(CString szPath)
{
CFileFind ff;
CString path = szPath;


if(path.Right(1) != "\\")
path += "\\";




path += "*.*";
BOOL res = ff.FindFile(path);




while(res)
{
res = ff.FindNextFile();
//是文件时直接删除
AfxMessageBox(ff.GetFilePath());
if (!ff.IsDots() && !ff.IsDirectory())
DeleteFile(ff.GetFilePath());
else if (ff.IsDots())
continue;
else if (ff.IsDirectory())
{
path = ff.GetFilePath();
//是目录时继续递归,删除该目录下的文件
RecursiveDelete(path);
//目录为空后删除目录
RemoveDirectory(path);
}
}
//最终目录被清空了,于是删除该目录
RemoveDirectory(szPath);
 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值