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);
 
}
/** ===================================================== 功能: CFolderContent类搜索一个目录, 列出该目录下的所有目录名称,列出所有子目录下的文件名称等属性。 作者: jef 作者邮箱: dungeonsnd@126.com 发步时间: 20100311 版本: v1.1 版权: 请遵循GNU. 对外接口: int GetAllSub(CString csPath) csPath: 一个目录或者一个完整的文件名 使用举例: void CGetFolderContentView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CString cs,csSaveFileName,csFileSave,csT,csT1; cs ="C:\\Documents and Settings\\All Users\\Documents\\My Music"; char chModule[8192]; memset(chModule,0,8192); GetModuleFileName(NULL,chModule,8192); csT.Format("%s",chModule); csT =csT.Left( csT.ReverseFind(_T('\\')) ); csSaveFileName =csT+_T("\\FolderContent输出文件.txt"); CRect rtClient; GetClientRect(&rtClient); CClientDC dc(this); dc.SetTextColor(RGB(200,80,80)); dc.FillSolidRect(rtClient,RGB(240,240,240)); CFolderContent fc; fc.GetAllSub(cs); int i,len,k; csT ="-------"; csT =cs+"总大小为"; csT1.Format(" %.4fMB,清单文件已保存在 %s",fc.m_dTotalSize/double(1024*1024),csSaveFileName); csT +=csT1; csT +="-------"; dc.TextOut(5,0,csT); csFileSave +=csT+_T("\r\n"); csT ="-------"; csT +="所有子目录如下:"; csT +="-------"; dc.TextOut(5,25,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_Directory.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecDirectorySize[i]/double(1024*1024)); csT1.Format(" 创建时间:%d年%d月%d日 ", fc.m_vecFileLastCreationTime[i].wYear, fc.m_vecFileLastCreationTime[i].wMonth, fc.m_vecFileLastCreationTime[i].wDay); csT =csT+csT1; csT =fc.m_Directory.GetAt(i)+csT; dc.TextOut(5,(i+2)*20,csT); csFileSave +=csT+_T("\r\n"); } k =i; csT ="-------"; csT +="所有文件如下:"; csT +="-------"; dc.TextOut(5,(k+3)*20,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_PathNameExt.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecFileSize[i]/double(1024*1024)); csT1.Format(" 修改时间:%d年%d月%d日 ", fc.m_vecFileLastWriteTime[i].wYear, fc.m_vecFile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值