现在的位置: 首页 > 综合 > 正文

计算文件夹的大小

2013年11月23日 ⁄ 综合 ⁄ 共 1153字 ⁄ 字号 评论关闭

没有直接的API函数,只有采用递归方法,下面这种判断是否是文件夹还是文件的方法与“删除文件夹”日志中的方法不一样,但是原理一样。

 

  1. LONGLONG llFolderSize; //全局变量
  2. CString OperateXML::calculateFolderSize( CString strFolderPath )
  3. {
  4.        CFileFind ff;
  5.        CString szDir = strFolderPath;
  6.        CString strSize = _T("");
  7.        if(szDir.Right(1) != "//")
  8.               szDir += "//";
  9.        
  10.        szDir += "*.*";
  11.        
  12.        BOOL res = ff.FindFile(szDir);
  13.        while(res)
  14.        {
  15.               res = ff.FindNextFile();
  16.               if(ff.IsDirectory() && !ff.IsDots())
  17.               {
  18.                      CString strFolderPath = ff.GetFilePath();
  19.                      calculateFolderSize(strFolderPath, nFileCount);
  20.               }
  21.               else if(!ff.IsDirectory() && !ff.IsDots())
  22.               {
  23.                      CString strFilePath;
  24.                      strFilePath = ff.GetFilePath();
  25.                      CFile curFile(strFilePath, CFile::modeRead);
  26.                      CFileStatus curFileStatus;
  27.                      curFile.GetStatus(curFileStatus);
  28.                      llFolderSize += curFileStatus.m_size;
  29.               }
  30.        }
  31.        ff.Close();
  32.        strSize.Format("%I64d", llFolderSize);
  33.        return strSize;
  34. }

 

抱歉!评论已关闭.