win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量
- #include <Windows.h>
- #include <stdio.h>
- #include <tchar.h>
- LARGE_INTEGER lgAllSubFolderFileSize;
- LARGE_INTEGER lgEachTempFileSize;
- DWORD dwTotalFolderNum=0;
- DWORD dwTotalFileNum=0;
- int ListFolderTotalFileSize(LPCTSTR lpTempPath)
- {
- TCHAR tchFindArray[MAX_PATH]= {_T("\0")};
- WIN32_FIND_DATA wFileDataAttr;
- BOOL bRet;
- _tcscpy_s(tchFindArray,MAX_PATH, lpTempPath);
- _tcscat_s(tchFindArray,_T("\\*.*"));
- HANDLE hFind= ::FindFirstFile(tchFindArray,&wFileDataAttr);
- if (INVALID_HANDLE_VALUE== hFind)
- {
- return -1;
- }
- while (TRUE)
- {
- if (wFileDataAttr.cFileName[0]!= _T('.'))
- {
- //_tprintf(_T("%s\\%s\n"),lpTempPath, findFileData.cFileName);
- lgEachTempFileSize.LowPart=wFileDataAttr.nFileSizeLow;
- lgEachTempFileSize.HighPart=wFileDataAttr.nFileSizeHigh;
- lgAllSubFolderFileSize.QuadPart=lgAllSubFolderFileSize.QuadPart+lgEachTempFileSize.QuadPart;
- dwTotalFileNum=dwTotalFileNum+1;
- if (wFileDataAttr.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)
- {
- _tcscpy_s(tchFindArray,MAX_PATH,lpTempPath);
- _tcscat_s(tchFindArray,_T("\\"));
- _tcscat_s(tchFindArray,wFileDataAttr.cFileName);
- dwTotalFolderNum=dwTotalFolderNum+1;
- ListFolderTotalFileSize(tchFindArray);
- }
- }
- bRet= ::FindNextFile(hFind,&wFileDataAttr);
- if (!bRet)
- {
- break;
- }
- }
- ::FindClose(hFind);
- return 1;
- }
- BOOL IsAscii2Unicode(const char * pstring,TCHAR pwText[])
- {
- DWORD dwLengthNum = MultiByteToWideChar (CP_ACP, 0, pstring, -1, NULL, 0);
- TCHAR *pwTempText = new TCHAR[dwLengthNum+1];
- ZeroMemory((PVOID)pwTempText,2*(dwLengthNum+1));
- if(pwTempText==NULL)
- {
- delete[] pwTempText;
- pwTempText=NULL;
- return FALSE;
- }
- if(0==MultiByteToWideChar (CP_ACP, 0, pstring, -1, (LPWSTR)pwTempText, dwLengthNum))
- {
- delete [] pwTempText;
- pwTempText=NULL;
- return FALSE;
- }
- else
- {
- lstrcpy(pwText,pwTempText);
- delete [] pwTempText;
- pwTempText=NULL;
- return TRUE;
- }
- }
- int main(int argc,_TCHAR * argv[])
- {
- TCHAR tchCommand0[MAX_PATH]={0};
- TCHAR tchCommand1[MAX_PATH]={0};
- TCHAR tchCommand2[MAX_PATH]={0};
- lgAllSubFolderFileSize.QuadPart=0L;
- lgEachTempFileSize.QuadPart=0L;
- BOOL bRet=IsAscii2Unicode((const char* )argv[0],tchCommand0);
- if (argc!=3)
- {
- wprintf(_T("example:\n"));
- wprintf(_T("\t"));
- wprintf(tchCommand0);
- wprintf(_T(" <-dir> d:\\temp\\\n"));
- return -1;
- }
- else
- {
- bRet=IsAscii2Unicode((const char* )argv[1],tchCommand1);
- if(bRet==FALSE)
- {
- wprintf(_T("argv convert fails\n"));
- return -1;
- }
- bRet=IsAscii2Unicode((const char* )argv[2],tchCommand2);
- if(bRet==FALSE)
- {
- wprintf(_T("argv convert fails\n"));
- return -1;
- }
- if((lstrcmp(tchCommand1,_T("-dir"))==0))
- //
- {
- DWORD dwRetSize=ListFolderTotalFileSize(tchCommand2);
- if (dwRetSize==-1)
- {
- _tprintf(_T("Open file failed \n"));
- return 1;
- }
- else
- {
- _tprintf(_T("NUMB SIZE : %u DIRES \n"),dwTotalFolderNum);
- _tprintf(_T("NUMB SIZE : %u FILES \n"),dwTotalFileNum);
- _tprintf(_T("FILE SIZE : %u BYTES \n"),lgAllSubFolderFileSize.QuadPart);
- //
- UINT uMaxSize=0;
- if(lgAllSubFolderFileSize.QuadPart<1024)
- {
- uMaxSize=lgAllSubFolderFileSize.QuadPart;
- _tprintf(_T("FILE SIZE : %.4u BYTE \n"),(uMaxSize));
- }
- else if ((lgAllSubFolderFileSize.QuadPart>1024)&&(lgAllSubFolderFileSize.QuadPart<1024*1024))
- {
- uMaxSize=lgAllSubFolderFileSize.QuadPart/1024;
- _tprintf(_T("FILE SIZE : %.4u KB \n"),(uMaxSize));
- }
- else if((lgAllSubFolderFileSize.QuadPart>1024*1024)&&(lgAllSubFolderFileSize.QuadPart<1024*1024*1024L))
- {
- uMaxSize=lgAllSubFolderFileSize.QuadPart/1024/1024;
- _tprintf(_T("FILE SIZE : %.4u MB \n"),(uMaxSize));
- }
- else
- {
- float fSize;
- fSize=(float)lgAllSubFolderFileSize.QuadPart/1024/1024/1024;
- _tprintf(_T("FILE SIZE : %.2f GB \n"),(fSize));
- }
- return 1;
- }
- }
- else
- {
- return 1;
- }
- }
- }
http://blog.csdn.net/microzone/article/details/49883783