Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to remove empty folders in a directory. boost::filesystem::recursive_directory_iterator can any one help me


C++
for(boost::filesystem::recursive_directory_iterator it(musicPaths); it != boost::filesystem::recursive_directory_iterator(); ++it)
{
    if ( is_directory(it->status()) )
    {

        //
        boost::filesystem::path pp = it->path();
        if( is_empty( pp))
        {
             // path removePath=it->path().filename();
            //string str = pp.string();

            bool bres=boost::filesystem3::remove(pp);
        //  boost::filesystem::current_path=pp.current_path();

            //pp=pp.parent_path();
        }


    }

}

the application crash.
Posted
Updated 7-Sep-12 0:13am
v3
Comments
Jochen Arndt 7-Sep-12 6:43am    
Where does it crash? Any messages?
You may add a try-catch block to your code:
try
{
// your code
}
catch (exception& e)
{
cout << e.what() << endl;
}
Chris Meech 7-Sep-12 13:25pm    
Just a guess but perhaps the call to is_epmty() returns true when there are no files but the path may contain other directories.
Himansu sekhar bal 10-Sep-12 1:14am    
ya u r right,

1 solution

I made a forum post ([Filesystem] remove and recursive_directory_iterator) on http://news.gmane.org/gmane.comp.lib.boost.user[^]. Not sure if it is a 'feature'. Turning the for loop into while and increment iterator before remove does the trick for me:

C++
fs::recursive_directory_iterator it(pthDirectory);
fs::recursive_directory_iterator itEnd;

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

   if (fs::is_directory(rPath, ec) && fs::is_empty(rPath, ec))
   {
      const fs::path pth = rPath;
      ++it;

      fs::remove(pth, ec);
   }
   else
   {
      ++it;
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900