On Apr 24, 3:34 pm, Paavo Helde <nob...@ebi.eewrote:
unkn...@example.com wrote innews:JT3Qj.2001$XI1.778@edtnps91:
I'm having files with a name pattern like "log_record-YY-MM-DD" and i
would like to create a list of the files available.
I'm wondering how i can do this, any ideas?
Look up the boost filesystem library, it should be able to do that.
Partially. IIRC, boost::filesystem::directory_iterator can be used to
enumerate the contents of a directory, but does not support filtering
the results. You have to take care of this yourself (using
boost::regex, for example).
So it would look like (warning: not tested, not compiled):
namespace bfs = boost::filesystem;
boost::regex filename_match("log_record(-\d{2}){3}");
std::list<bfs::pathfound_files;
for (bfs::directory_iterator it("path_to_search_in"); it !=
bfs::directory_iterator(); ++it){
if (boost::regex_match(*it, filename_match)){
found_files.push_back(*it);
}
}
For fun, while we're into Boost, we could wrap the directory_iterator
in a filter_iterator, applying the regex_match:
std::copy(
boost::make_filter_iterator(
boost::bind(&boost:regex_match, _1, filename_match),
bfs::directory_iterator("path_to_search_in")
),
boost::make_filter_iterator(
boost::bind(&boost:regex_match, _1, filename_match),
bfs::directory_iterator()
),
std::back_inserter(found_files)
);
But it becomes messy. You may want to define something like:
struct RegexMatch
{
RegexMatch() : m_R(""){}
RegexMatch(const char* Pattern): m_R(R){}
bool operator()(const bfs::path& P)const
{
return boost::regex_match(P.file_string(), m_R);
}
boost::regex m_R;
};
typedef boost::filter_iterator<RegexMatch, bfs::directory_iterator>
filtered_directory_iterator;
to enable usage like this:
std::copy(
filtered_directory_iterator(RegexMatch("log_record (-\d{2}){3}"),
bfs::directory_iterator("path_to_search_in")),
filtered_directory_iterator(),
std::back_inserter(found_files)
);