468,257 Members | 1,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

open files with specific pattern

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
--
weeks of software enineering safe hours of planing ;)
Jun 27 '08 #1
4 2578
On Apr 24, 12:59 pm, unkn...@example.com wrote:
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
--
weeks of software enineering safe hours of planing ;)
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.
Jun 27 '08 #2
un*****@example.com wrote in news:JT3Qj.2001$XI1.778@edtnps91:
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
Look up the boost filesystem library, it should be able to do that.

hth
Paavo
Jun 27 '08 #3
Christopher wrote:
On Apr 24, 12:59 pm, unkn...@example.com wrote:
>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
--
weeks of software enineering safe hours of planing ;)

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
--
weeks of software enineering safe hours of planing ;)
Jun 27 '08 #4
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)
);
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hokieghal99 | last post: by
7 posts views Thread by jonathandrott | last post: by
20 posts views Thread by Author | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.