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

问题描述

有没有办法让没有这个目录实际上遍历并增加它的每个文件的大小目录大小/文件夹的大小?理想的情况是想用这样的升压一些库,但赢得的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(!is_directory(*it))
            size+=bf::file_size(*it);
    }
}

PS:可以让这个用STD很多清洁::积累和拉姆达我只是CBA

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

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

08-05 02:40