does linux provide such a function? Thanks!
Printable View
does linux provide such a function? Thanks!
on the command lineCode:du -sh dirname
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.
Next door down...
http://cboard.cprogramming.com/forumdisplay.php?f=9
Moved to Linux Programming.
get all the files in the dir
do stat() on each file
accumulate the size
And use Boost.Filesystem's recursive_directory_iterator to do it. For example, if you're not worried about symlinks, you can do this:
Code:#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));
}