Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

c++ - boost::filesystem::recursive_directory_iterator with filter

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...