VC 遍历指定文件下的所有文件夹和文件 + 删除指定文件夹及下面所有文件及文件夹

本文介绍了一个用于遍历指定文件夹及其子文件夹中所有文件的方法,并提供了一个递归删除指定文件夹及其所有内容(包括子文件夹和文件)的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//遍历指定文件下的所有文件夹和文件://摘自 改进《遍历文件夹并建成目录树》;

void BrowseDir( CString strDir/*, HTREEITEM parent*/ )
{
 CFileFind ff;
 CString szDir = strDir;
// HTREEITEM hSubItem;
 
 if(szDir.Right(1) != "\\")
  szDir += "\\";
 
 szDir += "*.*";
 
 BOOL res = ff.FindFile(szDir);

 while( res )
 {
  res = ff.FindNextFile();

  if(ff.IsDirectory()  && !ff.IsDots() ) //文件夹
  {
   CString strPath = ff.GetFilePath();
   CString strTitle = ff.GetFileTitle();
   
 //  hSubItem = m_FileTree.InsertItem( strTitle, 0, 0, parent );
   cout<< strPath.GetBuffer(0) << endl;
   
   BrowseDir( strPath/*, hSubItem*/ );
  }
  else if(!ff.IsDirectory() && !ff.IsDots() ) //文件:
  {
   CString strTitle = ff.GetFileTitle();
   
   // m_FileTree.InsertItem( strTitle, 0, 0, parent );

    cout<< ff.GetFilePath().GetBuffer(0) << endl;
  }
 }
 ff.Close();
}

 

//删除指定文件夹及下面所有文件及文件夹

BOOL DeleteDirectory(char *sDirName)
{
    CFileFind tempFind;
    char sTempFileFind[200] = {0} ;
 
    sprintf(sTempFileFind,"%s\\*.*",sDirName);
    BOOL IsFinded = tempFind.FindFile(sTempFileFind);
    while (IsFinded)
    {
        IsFinded = tempFind.FindNextFile();
  
        if (!tempFind.IsDots())
        {
            char sFoundFileName[200];
            strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));
   
            if (tempFind.IsDirectory())
            {
                char sTempDir[200];
                sprintf(sTempDir,"%s\\%s",sDirName,sFoundFileName);
                DeleteDirectory(sTempDir);
            }
            else
            {
                char sTempFileName[200];
                sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName);
                DeleteFile(sTempFileName);
            }
        }
    }
    tempFind.Close();
    if(!RemoveDirectory(sDirName))
    {
        return FALSE;
    }
    return TRUE;
}



/** ===================================================== 功能: 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、付费专栏及课程。

余额充值