赞助

获取文件或是文件夹的大小和占用空间

 1 //Get size of a whole dir
 2 //return bytes
 3 DWORD64 GetFolderSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders)
 4 {
 5  TCHAR szFileFilter[512];
 6  TCHAR szFilePath[512];
 7  HANDLE hFind = NULL;
 8  WIN32_FIND_DATA fileinfo;
 9  DWORD64    dwSize = 0;
10  
11  strcpy(szFilePath,szPath);
12  strcat(szFilePath,"\");
13  strcpy(szFileFilter,szFilePath);
14  strcat(szFileFilter,"*.*");
15  
16  hFind = FindFirstFile(szFileFilter,&fileinfo);
17  do
18  {
19   if(fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
20   {
21    if (!strcmp(fileinfo.cFileName,".") || !strcmp(fileinfo.cFileName,".."))
22    {
23     //Do nothing for "." and ".." folders
24    }
25    else
26    {
27     TCHAR sztmp[512];
28     strcpy(sztmp,szFilePath);
29     strcat(sztmp,fileinfo.cFileName);
30     dwSize = dwSize + GetFolderSize(sztmp);
31     if(dwFolders != NULL)
32     {
33      ++(*dwFolders);
34     }
35    }
36   }
37   else
38   {
39    if(dwFiles != NULL)
40    {
41     ++(*dwFiles);
42    }
43   }
44  
45   dwSize += fileinfo.nFileSizeLow;
46  
47  }while(FindNextFile(hFind,&fileinfo));
48  
49  FindClose(hFind);
50  return dwSize;
51 }
posted @ 2016-01-20 16:22  车臣  阅读(774)  评论(0编辑  收藏  举报