zoukankan      html  css  js  c++  java
  • 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("")};  
    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 "),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: "));  
    87.         wprintf(_T(" "));  
    88.         wprintf(tchCommand0);  
    89.         wprintf(_T(" <-dir> d:\temp\ "));  
    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 "));  
    98.             return -1;  
    99.         }  
    100.         bRet=IsAscii2Unicode((const char* )argv[2],tchCommand2);  
    101.         if(bRet==FALSE)  
    102.         {  
    103.             wprintf(_T("argv convert fails "));  
    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  "));  
    113.                 return 1;  
    114.             }   
    115.             else  
    116.             {  
    117.                   
    118.                 _tprintf(_T("NUMB SIZE  : %u DIRES  "),dwTotalFolderNum);   
    119.                 _tprintf(_T("NUMB SIZE  : %u FILES  "),dwTotalFileNum);   
    120.                 _tprintf(_T("FILE SIZE  : %u BYTES  "),lgAllSubFolderFileSize.QuadPart);  
    121.                 //  
    122.                 UINT uMaxSize=0;  
    123.                 if(lgAllSubFolderFileSize.QuadPart<1024)  
    124.                 {  
    125.                     uMaxSize=lgAllSubFolderFileSize.QuadPart;  
    126.                     _tprintf(_T("FILE SIZE : %.4u BYTE  "),(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  "),(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  "),(uMaxSize));   
    137.                 }  
    138.                 else   
    139.                 {  
    140.                     float fSize;  
    141.                     fSize=(float)lgAllSubFolderFileSize.QuadPart/1024/1024/1024;  
    142.                     _tprintf(_T("FILE SIZE : %.2f GB  "),(fSize));   
    143.                 }  
    144.                 return 1;  
    145.             }  
    146.         }  
    147.         else  
    148.         {  
    149.             return 1;  
    150.         }  
    151.     }  
    152. }  

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

  • 相关阅读:
    python通过标准输入读取内容,读取键盘输入的内容?接收用户输入?
    Centos修改时间显示的时区,将UTC修改为CST
    python中注释的写法
    python中,数字类型计算
    python中,获取字符串的长度
    python中的字符串常量,是否支持通过下标的方式赋值
    python,如何获取字符串中的子字符串,部分字符串
    python中是否有单独的字符类型,通过下标的方式表示字符串中的字符
    python中,如何将两个变量值进行拼接
    python中,如有个非常长的字符串,在写的时候如何将其分隔
  • 原文地址:https://www.cnblogs.com/findumars/p/8306982.html
Copyright © 2011-2022 走看看