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;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值