How can I find the size of all files located inside a folder?

后端 未结 12 2383
萌比男神i
萌比男神i 2020-12-06 05:49

Is there any API in c++ for getting the size of a specified folder?

If not, how can I get the total size of a folder including all subfolders and files?

12条回答
  •  情歌与酒
    2020-12-06 06:22

    5 years and not a simple solution with standard C++, that's why I would like to contribute my solution to this question:

    uint64_t GetDirSize(const std::string &path)
    {
        uint64_t size = 0;
        for (const auto & entry : std::experimental::filesystem::directory_iterator(path))
        {
            if(entry.status().type() == std::experimental::filesystem::file_type::regular)
                size += std::experimental::filesystem::file_size(entry.path());
            if (entry.status().type() == std::experimental::filesystem::file_type::directory)
                size += GetDirSize(entry.path().generic_string());
        }
        return size;
    }
    

    Use it for example by calling GetDirSize("C:\\dir_name") if you're using Windows.

提交回复
热议问题