c++ - Can I use a mask to iterate files in a directory with Boost? -
i want iterate on files in directory matching "somefiles*.txt". boost::filesystem have built in that, or need regex or against each leaf()?
edit: noted in comments, code below valid versions of boost::filesystem
prior v3. v3, refer suggestions in comments.
boost::filesystem
not have wildcard search, have filter files yourself.
this code sample extracting content of directory boost::filesystem
's directory_iterator
, filtering boost::regex
:
const std::string target_path( "/my/directory/" ); const boost::regex my_filter( "somefiles.*\.txt" ); std::vector< std::string > all_matching_files; boost::filesystem::directory_iterator end_itr; // default ctor yields past-the-end for( boost::filesystem::directory_iterator i( target_path ); != end_itr; ++i ) { // skip if not file if( !boost::filesystem::is_regular_file( i->status() ) ) continue; boost::smatch what; // skip if no match v2: if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue; // v3: //if( !boost::regex_match( i->path().filename(), what, my_filter ) ) continue; // file matches, store all_matching_files.push_back( i->leaf() ); }
(if looking ready-to-use class builtin directory filtering, have @ qt's qdir
.)
Comments
Post a Comment