Click here to Skip to main content
11,933,854 members (55,622 online)
Rate this:
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();


the application crash.
Posted 7-Sep-12 1:07am
Edited 7-Sep-12 1:13am
Jochen Arndt 7-Sep-12 6:43am
Where does it crash? Any messages?
You may add a try-catch block to your code:
// 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.
c++-thinker 10-Sep-12 1:14am
ya u r right,

1 solution

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

Solution 1

I made a forum post ([Filesystem] remove and recursive_directory_iterator) on[^]. 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;
      fs::remove(pth, ec);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 9 Jun 2014
Copyright © CodeProject, 1999-2015
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