win32 文件夹遍历获取每个文件夹大小
Posted fulianzhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win32 文件夹遍历获取每个文件夹大小相关的知识,希望对你有一定的参考价值。
#include <stdint.h>
#include <list>
#include <windows.h>
using namespace std;
#define MAX_PATH_LEN (8192)
#define G_UNIT (1024*1024*1024)
#define M_UNIT (1024*1024)
#define K_UNIT (1024)
char* BytesExchange(uint64_t bytes)
static char str[256] = 0 ;
if (bytes >= G_UNIT)
sprintf_s(str, sizeof(str), "%.2fG", bytes*1.0 / G_UNIT);
else if (bytes >= M_UNIT)
sprintf_s(str, sizeof(str), "%.2fM", bytes*1.0 / M_UNIT);
else if (bytes >= K_UNIT)
sprintf_s(str, sizeof(str), "%.2fK", bytes*1.0 / K_UNIT);
else
sprintf_s(str, sizeof(str), "%dbytes", bytes);
return str;
uint64_t TraverFolder(const char* dirname)
uint64_t TotalSize = 0;
list<string> dirlist;
dirlist.push_back(dirname);
while (!dirlist.empty())
string dirstr = dirlist.front();
dirlist.pop_front();
char findName[MAX_PATH_LEN] = 0 ;
strcpy(findName, dirstr.c_str());
strcat(findName, "\\\\*");
uint64_t DirSize = 0;
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA(findName, &wfd);
while (hFind && INVALID_HANDLE_VALUE != hFind)
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
if (strcmp(wfd.cFileName, ".") != 0 &&
strcmp(wfd.cFileName, "..") != 0)
char FolderName[MAX_PATH_LEN] = 0 ;
sprintf_s(FolderName, sizeof(FolderName), "%s\\\\%s", dirstr.c_str(), wfd.cFileName);
dirlist.push_back(std::string(FolderName));
else
uint64_t FileSize = (uint64_t)wfd.nFileSizeHigh << 32 | (uint32_t)wfd.nFileSizeLow;
DirSize += FileSize;
if (!FindNextFileA(hFind, &wfd))
break;
//printf(" %s(%d) (%s)\\n", dirstr.c_str(), dirstack.size(), BytesExchange(DirSize));
TotalSize += DirSize;
return TotalSize;
int main(int argc, char* argv[])
char dirName[] = "C:\\\\Windows";
char findName[MAX_PATH_LEN] = 0 ;
strcpy(findName, dirName);
strcat(findName, "\\\\*");
uint64_t DirSize = 0;
WIN32_FIND_DATAA wfd;
HANDLE hFind = FindFirstFileA(findName, &wfd);
while (hFind && hFind != INVALID_HANDLE_VALUE)
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
if (strcmp(wfd.cFileName, ".") != 0 &&
strcmp(wfd.cFileName, "..") != 0)
char FolderName[MAX_PATH_LEN] = 0 ;
sprintf_s(FolderName, sizeof(FolderName), "%s\\\\%s", dirName, wfd.cFileName);
uint64_t TotalSize = TraverFolder(FolderName);
printf("%s (%s)\\n", FolderName, BytesExchange(TotalSize));
if (!FindNextFileA(hFind, &wfd))
break;
return 0;
以上是关于win32 文件夹遍历获取每个文件夹大小的主要内容,如果未能解决你的问题,请参考以下文章
perl win32 api 如何遍历已打开窗口,获得每个窗口的标题内容