Is there a way to get a linux directory size in c++? [Archive] - C Board

PDA

View Full Version : Is there a way to get a linux directory size in c++?


meili100
04-18-2008, 03:55 PM
does linux provide such a function? Thanks!

cyberfish
04-18-2008, 04:03 PM
du -sh dirname

on the command line

I don't think it's possible to do it directly in C++. You will probably have to write a recursive function to do it.

medievalelks
04-18-2008, 04:23 PM
does linux provide such a function? Thanks!

Next door down...

http://cboard.cprogramming.com/forumdisplay.php?f=9

laserlight
04-18-2008, 11:55 PM
Moved to Linux Programming.

Salem
04-19-2008, 12:25 AM
get all the files in the dir
do stat() on each file
accumulate the size

CornedBee
04-19-2008, 02:01 AM
And use Boost.Filesystem's recursive_directory_iterator to do it. For example, if you're not worried about symlinks, you can do this:
#include <boost/cstdint.hpp>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/bind.hpp>
#include <boost/iterators/transform_iterator.hpp>
namespace fs = boost::filesystem;

boost::uintmax_t dir_size(const fs::path &p)
{
return std::accumulate(
boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator(p)),
boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator()),
static_cast<boost::uintmax_t>(0));
}