Boost logo

Boost Users :

Subject: [Boost-users] [Filesystem] remove and recursive_directory_iterator
From: gast128 (gast128_at_[hidden])
Date: 2014-06-09 08:23:18


I couldn't find a description of the iterator stability of
recursive_directory_iterator but it gives problems when you remove the
directory which is being iterated even when that directory is empty. For
example if you want to remove all empty directories, the following code will
throw an exception in ++it after the 'remove' call:

fs::path pthDirectory = ...;

fs::recursive_directory_iterator itEnd;

for (fs::recursive_directory_iterator it(pthDirectory); it != itEnd; ++it)
{
   boost::system::error_code ec;
   const fs::path& rPath = it->path();

   if (fs::is_directory(rPath, ec))
   {
      if (fs::is_empty(rPath, ec))
      {
         fs::remove(rPath, ec);
      }
   }
}

It seems solvable with the way one erases a map (i.e. using while (it !=
itEnd) and using the equivalent of map.erase(it++)).

Maybe add a note to the documentation or did I overlook something?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net