目录大小 [英] Size of a directory

查看:17
本文介绍了目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不实际遍历此目录并添加其中每个文件的大小的情况下获取目录大小/文件夹大小?理想情况下想使用一些像 boost 这样的库,但 win api 也可以.

Is there a way to get the directory size/folder size without actually traversing this directory and adding size of each file in it? Ideally would like to use some library like boost but win api would be ok too.

推荐答案

据我所知,您必须在大多数操作系统上通过迭代来做到这一点.

As far as I am aware you have to do this with iteration on most operating systems.

你可以看一下 boost.filesystem,这个库有一个 recursive_directory_iterator,它会迭代,尽管系统上的任何文件都得到了累积的大小.

You could take a look at boost.filesystem, this library has a recursive_directory_iterator, it will iterate though ever file on the system getting accumulation the size.

http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-recursive_directory_iterator

include <boost/filesystem.hpp>
int main()
{
    namespace bf=boost::filesystem;
    size_t size=0;
    for(bf::recursive_directory_iterator it("path");
        it!=bf::recursive_directory_iterator();
        ++it)
    {   
        if(!bf::is_directory(*it))
            size+=bf::file_size(*it);
    }   
}

PS:你可以通过使用 std::accumulate 和一个 lambda 我只是 CBA 使这更干净

PS: you can make this a lot cleaner by using std::accumulate and a lambda I just CBA

这篇关于目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆