473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2755
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all...
2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
29
by: Tola | last post by:
In my case of study, my teacher gave me a project, after I analysed the problem I found that I had to used open the file on the other machine? Please help? Thank you in advance. Tola CHROUK
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
7
by: jonathandrott | last post by:
sorry newbie question probably. i'm trying to open an specific folder. open each file with in the folder individually and process each one. all the processing code has been written. i'm looking...
5
by: krisbee1983 | last post by:
Hello to all, I'm beginer in learning Python I wish somebody help me with solving this problem. I would like to read all text files wchich are in some folder. For this text files I need to make...
2
jaccess
by: jaccess | last post by:
I was wondering if there is a simple way to open a specific folder from an access form button. I have a form that a user can enter a date range and either view or print reports based on the date...
1
by: deve8ore | last post by:
Hello, I have a fairly simple macro where I can guide the user to the correct directory and file path, however I would like the file with a specific extension (in this example it's an Output file, or...
20
by: Author | last post by:
a .net 1.1 app has a class whose constructor opens a db connection to sql svr two thousand. this class has more than a dozen of methods. most of them don't do db stuff. I am wondering if this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.