Can I use a mask to iterate files in a directory with Boost?

后端 未结 7 1111
既然无缘
既然无缘 2020-11-27 03:51

I want to iterate over all files in a directory matching something like somefiles*.txt.

Does boost::filesystem have something built in to

7条回答
  •  無奈伤痛
    2020-11-27 04:31

    There is a Boost Range Adaptors way:

    #define BOOST_RANGE_ENABLE_CONCEPT_ASSERT 0
    #include 
    #include 
    
    namespace bfs = boost::filesystem;
    namespace ba = boost::adaptors;
    
    const std::string target_path( "/my/directory/" );
    const boost::regex my_filter( "somefiles.*\.txt" );
    boost::smatch what;
    
    for (auto &entry: boost::make_iterator_range(bfs::directory_iterator(target_path), {})
    | ba::filtered(static_cast(&bfs::is_regular_file))
    | ba::filtered([&](const bfs::path &path){ return boost::regex_match(path.filename().string(), what, my_filter); })
    )
    {
      // There are only files matching defined pattern "somefiles*.txt".
      std::cout << entry.path().filename() << std::endl;
    }
    

提交回复
热议问题