win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量

[cpp] view plain copy
 
 print?
  1. #include <Windows.h>  
  2. #include <stdio.h>  
  3. #include <tchar.h>  
  4.   
  5. LARGE_INTEGER       lgAllSubFolderFileSize;  
  6. LARGE_INTEGER       lgEachTempFileSize;  
  7. DWORD               dwTotalFolderNum=0;  
  8. DWORD               dwTotalFileNum=0;  
  9.   
  10. int ListFolderTotalFileSize(LPCTSTR lpTempPath)  
  11. {  
  12.     TCHAR tchFindArray[MAX_PATH]= {_T("\0")};  
  13.     WIN32_FIND_DATA wFileDataAttr;  
  14.     BOOL bRet;  
  15.     _tcscpy_s(tchFindArray,MAX_PATH, lpTempPath);  
  16.     _tcscat_s(tchFindArray,_T("\\*.*"));       
  17.     HANDLE hFind= ::FindFirstFile(tchFindArray,&wFileDataAttr);  
  18.     if (INVALID_HANDLE_VALUE== hFind)  
  19.     {  
  20.         return -1;  
  21.     }  
  22.     while (TRUE)  
  23.     {  
  24.         if (wFileDataAttr.cFileName[0]!= _T('.'))  
  25.         {  
  26.             //_tprintf(_T("%s\\%s\n"),lpTempPath, findFileData.cFileName);  
  27.             lgEachTempFileSize.LowPart=wFileDataAttr.nFileSizeLow;  
  28.             lgEachTempFileSize.HighPart=wFileDataAttr.nFileSizeHigh;  
  29.             lgAllSubFolderFileSize.QuadPart=lgAllSubFolderFileSize.QuadPart+lgEachTempFileSize.QuadPart;  
  30.             dwTotalFileNum=dwTotalFileNum+1;  
  31.             if (wFileDataAttr.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)  
  32.             {  
  33.                 _tcscpy_s(tchFindArray,MAX_PATH,lpTempPath);     
  34.                 _tcscat_s(tchFindArray,_T("\\"));     
  35.                 _tcscat_s(tchFindArray,wFileDataAttr.cFileName);  
  36.                 dwTotalFolderNum=dwTotalFolderNum+1;  
  37.                 ListFolderTotalFileSize(tchFindArray);  
  38.             }  
  39.         }  
  40.         bRet= ::FindNextFile(hFind,&wFileDataAttr);  
  41.         if (!bRet)  
  42.         {  
  43.             break;  
  44.         }  
  45.     }  
  46.     ::FindClose(hFind);  
  47.     return 1;  
  48. }  
  49.   
  50. BOOL IsAscii2Unicode(const char * pstring,TCHAR pwText[])  
  51. {  
  52.     DWORD dwLengthNum = MultiByteToWideChar (CP_ACP, 0, pstring, -1, NULL, 0);  
  53.     TCHAR *pwTempText = new TCHAR[dwLengthNum+1];  
  54.     ZeroMemory((PVOID)pwTempText,2*(dwLengthNum+1));  
  55.     if(pwTempText==NULL)  
  56.     {  
  57.         delete[] pwTempText;  
  58.         pwTempText=NULL;  
  59.         return FALSE;  
  60.     }  
  61.     if(0==MultiByteToWideChar (CP_ACP, 0, pstring, -1, (LPWSTR)pwTempText, dwLengthNum))  
  62.     {  
  63.         delete [] pwTempText;  
  64.         pwTempText=NULL;  
  65.         return FALSE;  
  66.     }  
  67.     else  
  68.     {  
  69.         lstrcpy(pwText,pwTempText);  
  70.         delete [] pwTempText;  
  71.         pwTempText=NULL;  
  72.         return TRUE;  
  73.     }  
  74. }  
  75.   
  76. int  main(int argc,_TCHAR * argv[])  
  77. {  
  78.     TCHAR tchCommand0[MAX_PATH]={0};  
  79.     TCHAR tchCommand1[MAX_PATH]={0};  
  80.     TCHAR tchCommand2[MAX_PATH]={0};  
  81.     lgAllSubFolderFileSize.QuadPart=0L;  
  82.     lgEachTempFileSize.QuadPart=0L;  
  83.     BOOL bRet=IsAscii2Unicode((const char* )argv[0],tchCommand0);  
  84.     if (argc!=3)  
  85.     {  
  86.         wprintf(_T("example:\n"));  
  87.         wprintf(_T("\t"));  
  88.         wprintf(tchCommand0);  
  89.         wprintf(_T(" <-dir> d:\\temp\\\n"));  
  90.         return -1;  
  91.     }  
  92.     else  
  93.     {  
  94.         bRet=IsAscii2Unicode((const char* )argv[1],tchCommand1);  
  95.         if(bRet==FALSE)  
  96.         {  
  97.             wprintf(_T("argv convert fails\n"));  
  98.             return -1;  
  99.         }  
  100.         bRet=IsAscii2Unicode((const char* )argv[2],tchCommand2);  
  101.         if(bRet==FALSE)  
  102.         {  
  103.             wprintf(_T("argv convert fails\n"));  
  104.             return -1;  
  105.         }  
  106.         if((lstrcmp(tchCommand1,_T("-dir"))==0))  
  107.             //  
  108.         {  
  109.             DWORD dwRetSize=ListFolderTotalFileSize(tchCommand2);  
  110.             if (dwRetSize==-1)  
  111.             {  
  112.                 _tprintf(_T("Open file failed \n"));  
  113.                 return 1;  
  114.             }   
  115.             else  
  116.             {  
  117.                   
  118.                 _tprintf(_T("NUMB SIZE  : %u DIRES \n"),dwTotalFolderNum);   
  119.                 _tprintf(_T("NUMB SIZE  : %u FILES \n"),dwTotalFileNum);   
  120.                 _tprintf(_T("FILE SIZE  : %u BYTES \n"),lgAllSubFolderFileSize.QuadPart);  
  121.                 //  
  122.                 UINT uMaxSize=0;  
  123.                 if(lgAllSubFolderFileSize.QuadPart<1024)  
  124.                 {  
  125.                     uMaxSize=lgAllSubFolderFileSize.QuadPart;  
  126.                     _tprintf(_T("FILE SIZE : %.4u BYTE \n"),(uMaxSize));   
  127.                 }  
  128.                 else if ((lgAllSubFolderFileSize.QuadPart>1024)&&(lgAllSubFolderFileSize.QuadPart<1024*1024))  
  129.                 {  
  130.                     uMaxSize=lgAllSubFolderFileSize.QuadPart/1024;  
  131.                     _tprintf(_T("FILE SIZE : %.4u KB \n"),(uMaxSize));   
  132.                 }  
  133.                 else if((lgAllSubFolderFileSize.QuadPart>1024*1024)&&(lgAllSubFolderFileSize.QuadPart<1024*1024*1024L))  
  134.                 {  
  135.                     uMaxSize=lgAllSubFolderFileSize.QuadPart/1024/1024;  
  136.                     _tprintf(_T("FILE SIZE : %.4u MB \n"),(uMaxSize));   
  137.                 }  
  138.                 else   
  139.                 {  
  140.                     float fSize;  
  141.                     fSize=(float)lgAllSubFolderFileSize.QuadPart/1024/1024/1024;  
  142.                     _tprintf(_T("FILE SIZE : %.2f GB \n"),(fSize));   
  143.                 }  
  144.                 return 1;  
  145.             }  
  146.         }  
  147.         else  
  148.         {  
  149.             return 1;  
  150.         }  
  151.     }  
  152. }  

http://blog.csdn.net/microzone/article/details/49883783

posted @ 2018-01-18 00:48  findumars  Views(1339)  Comments(0Edit  收藏  举报