I want to iterate over all files in a directory matching something like "somefiles*.txt". Does boost::filesystem have something built in to do that, or do I need a regex or something against each leaf()?
A:
I believe the directory_iterators will only provide all files in a directory. It up to you to filter them as necessary.
jon hanson
2009-08-11 00:41:24
+7
A:
boost::filesystem does not have wildcard search, you have to filter files yourself.
This is a code sample extracting the content of a directory with a boost::filesystem's directory_iterator and filtering it with 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 ); i != end_itr; ++i )
{
// Skip if not a file
if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
boost::smatch what;
// Skip if no match
if( !boost::regex_match( i->leaf(), what, my_filter ) ) continue;
// File matches, store it
all_matching_files.push_back( i->leaf() );
}
(If you are looking for a ready-to-use class with builtin directory filtering, have a look at Qt's QDir.)
Julien L.
2009-08-11 09:06:41