Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::filesystem::recursive_directory_iterator with filter

Tags:

c++

boost

I need to recursively get all files from directory and it's subdirectory, but excluding several directory. I know their names. Is it possible to do with boost::filesystem::recursive_directory_iterator ?

like image 490
Borrimoro Avatar asked Aug 14 '13 13:08

Borrimoro


1 Answers

Yes, while iterating over directories, you can test for the names on your exclusion list and use the no_push() member of the recursive iterator to prevent it from going into such a directory, something like:

void selective_search( const path &search_here, const std::string &exclude_this_directory)
{
    using namespace boost::filesystem;
    recursive_directory_iterator dir( search_here), end;
    while (dir != end)
    {
        // make sure we don't recurse into certain directories
        // note: maybe check for is_directory() here as well...
        if (dir->path().filename() == exclude_this_directory)
        {
            dir.no_push(); // don't recurse into this directory.
        }

        // do other stuff here.            

        ++dir;
    }
 }
like image 200
dhavenith Avatar answered Nov 15 '22 15:11

dhavenith