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

后端 未结 7 1124
既然无缘
既然无缘 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条回答
  •  萌比男神i
    2020-11-27 04:11

    The accepted answer did not compile for me even when I used i->path().extension() instead of leaf(). What did work for me was an example from this website. Here's the code, modified, to apply a filter:

    vector results;
    filesystem::path filepath(fullpath_to_file);
    filesystem::directory_iterator it(filepath);
    filesystem::directory_iterator end;
    const boost::regex filter("myfilter(capturing group)");
    BOOST_FOREACH(filesystem::path const &p, make_pair(it, end))
    {
         if(is_regular_File(p))
         {
              match_results what;
              if (regex_search(it->path().filename().string(), what, pidFileFilter, match_default))
              {
                   string res = what[1];
                   results.push_back(res);
              }
         }
    }
    

    I'm using Boost version: 1.53.0.

    Why we don't all just use glob() and some regex is beyond me.

提交回复
热议问题