Removing a directory with Boost - C++ boost

C++ examples for boost:file system

Description

Removing a directory with Boost

#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>

using namespace std;
using namespace boost::filesystem;

void removeRecurse(const path& p) {
   directory_iterator end;
   for (directory_iterator it(p);
        it != end; ++it) {

      if (is_directory(*it)) {
         removeRecurse(*it);
      }
      else {
         remove(*it);
      }
   }
   remove(p);
}

int main(int argc, char** argv) {
   path thePath = system_complete(path("newFolder", native));

   if (!exists(thePath)) {
      cerr << "Error: the directory " << thePath.string()<< " does not exist.\n";
      return(EXIT_FAILURE);
   }

   try {
      removeRecurse(thePath);
   }
   catch (exception& e) {
      cerr << e.what() << endl;
      return(EXIT_FAILURE);
   }
   return(EXIT_SUCCESS);
}

Related Tutorials