Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
how to remove empty folders in a directory. boost::filesystem::recursive_directory_iterator can any one help me
 

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 7-Sep-12 1:07am
Edited 7-Sep-12 1:13am
v3
Comments
Jochen Arndt at 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 at 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.
c++-thinker at 10-Sep-12 1:14am
   
ya u r right,

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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;
   }
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 329
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 9 Jun 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100