HOWTO: Directory recursion in Boost (and other tips)

Boost's Filesystem library is an incredible resource: it abstracts paths, directories, and stat results. It simplifies coding shell problems in C++, it's portable, and is maintained by a large community of contributors. The one downside of Boost is that some of its newer libraries are poorly documented. ((Something I want to contribute to.)) Until I have time to get involved in the Boost project, I'm going to post examples here.

Traversing a directory tree

This is the coolest feature I've found in boost::filesystem so far. It treats directory elements like iterators, and has a convenience iterator that flattens the problem of iterating through a directory tree recursively. The only examples I found for it were in their extensive test sources, (See convenience tests and operations tests for more examples.) which are a bit light on comments.

#include "boost/filesystem.hpp"
#include <iostream>

for ( boost::filesystem::recursive_directory_iterator end, dir("./"); 
       dir != end; ++dir ) {
       cout << *dir << std::endl;                                    
}

The example starts in the current working directory, and prints all of the file names (and directories) in inode order. ((Later I'll try to post an example that uses the stat features to dump extra information.))

Notes:

Aliasing Boost namespaces

Here's how Boost's own source recommends aliasing their namespaces:

namespace fs = boost::filesystem;
namespace sys = boost::system;

Other cool bits

I'll write more about these later, but for now here are a few things you'll find in the library:

Things that are great about boost::filesystem's approach:

#boost #c++