4

I want to iterate over all files in a directory matching something "keyword.txt". I searched for some solutions in google and found this: Can I use a mask to iterate files in a directory with Boost?

As i figured out later on, the "leaf()" function was replaced (source: http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/index.htm -> goto section 'Deprecated names and features')

what i got so far is this, but it's not running. Sorry for this somehow stupid question, but im more or less a c++ beginner.

    const std::string target_path( "F:\\data\\" );
const boost::regex my_filter( "keyword.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->path().filename(), what, my_filter ) ) continue;

    // File matches, store it
    all_matching_files.push_back( i->path().filename() );
}
Community
  • 1
  • 1
user2003965
  • 473
  • 8
  • 19

1 Answers1

4

Try

i->path().filename().string()

this is the equivalent for i->leaf() in boost::filesystem 3.0

In your code:

// Skip if no match
if( !boost::regex_match( i->path().filename().string(), what, my_filter ) )     
    continue;

// File matches, store it
all_matching_files.push_back( i->path().filename().string() ); 
cpp
  • 3,743
  • 3
  • 24
  • 38
  • Thanks, but this gives me an error with the pushback function – user2003965 Sep 04 '13 at 18:06
  • Anyhow the problem with this approach is, that i will need to search for different things very often the programm, so i would prefer handing over a "keyword" string. – user2003965 Sep 04 '13 at 18:13
  • Second time, thanks a lot - i even understand the mistake :) It runs, but it doesnt worked in the wished manner. I got some files names "data1.txt", "data2.txt" ... in the target_path folder. If i enter my_filter("1") it doesn't find the "data1.txt". (I'm sure that im working in the right dict) Any idea where this comes from? – user2003965 Sep 04 '13 at 18:56