open files with specific pattern

U

unknown

Hi,

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?

Thank you!
Ron
 
C

Christopher

Hi,

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?

Thank you!
Ron

There are some OS specific functions availabe for this, which would
probably be the best way. Research "C++ file i/o and <insert your OS
here>" on Google.

There may be a way to do it solely using standard C++, by attempting
to open fstreams with different names and checking for failure, but it
would most likely be far more complex. I wouldn't even want to think
about it.
 
R

Ron Eggler

Christopher said:
There are some OS specific functions availabe for this, which would
probably be the best way. Research "C++ file i/o and <insert your OS
here>" on Google.

There may be a way to do it solely using standard C++, by attempting
to open fstreams with different names and checking for failure, but it
would most likely be far more complex. I wouldn't even want to think
about it.

Hi Christiopher,

I tried doing it this way:

int PRGDaemon::getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}

while ((dirp = readdir(dp)) != NULL &&
string(dirp->d_name).find("log_record")!=string::npos) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}

Doesn't work quite yet, trying to figure out why not. But I belive opendir
and readdir should be the way to go.

Thanks for any help or suggestions!
Ron
 
E

Eric.Malenfant

(e-mail address removed) wrote innews:JT3Qj.2001$XI1.778@edtnps91:

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::path> found_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)
);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top