find files depending on substr and delete them? UNIX + C++

Iam new to unix and c++.. here's my problem: I have some files constantly generated in seq like
ABU030227154405  ABU030227154910  ABU030227165702

i need to find the files which match some string like "227" and delete all the files that have that string in the filename.. its like "rm ABU03227* ".... how do i do it ?? i am kind of lost. there are 2 methods i could think of one using remove() and the other through system().. anyone with a good clean code to solve this ??


void delete_file( )
{
   int cur_year, cur_month, cur_day;
   struct tm *time_ptr;
   time_t lt;
   lt = time('\0');
   bool leapyear;
   int total_days;
   int days[13] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

 /*
 //find all files matching substr
 //get the name of those files
 if (remove(files)!= 0){
    cout <<"cant removed file \n";
 }
 */
 string rmstr(" rm ");
 string filestr("ABU030227*");
 string remove_cmd = rmstr + filestr;
 cout <<"the STRING IS ="<<remove_cmd<<endl;
 if(system(remove_cmd.c_str())== -1){
    cout <<"cant removed file \n";
 }
}
mrb28Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
What about using 'opendir()' and 'readdir()'?

#include <sys/types.h>
#include <dirent.h>

DIR* pDir = opendir ( ".");

dirent* pEntry;

while ( pEntry = readdir ( pDir)) {

 if ( DT_REG != pDir->d_type) continue; // skip non-regular files

 // does file match the criteria?
 if ( strstr ( pDir->d_name, "227")) {
 // yes, delete it
  unlink ( pDir->d_name);
 }

 closedir ( pDir);

}
0
mrb28Author Commented:
what is DT_REG and d_type.. i get compilation errors when i use the code. where do i find the structure of DIR ?

line 234: Error: DT_REG is not defined.
line 234: Error: d_type is not a member of DIR.
Error: d_name is not a member of DIR.
line 239: Error: d_name is not a member of DIR.
4 Error(s) detected.

d_name maybe be member of dirent* pEntry instead.
0
jkrCommented:
Ooops, you are right - typo :o)

it should read


#include <sys/types.h>
#include <dirent.h>

DIR* pDir = opendir ( ".");

dirent* pEntry;

while ( pEntry = readdir ( pDir)) {

if ( DT_REG != pEntry->d_type) continue; // skip non-regular files

// does file match the criteria?
if ( strstr ( pEntry->d_name, "227")) {
// yes, delete it
 unlink ( pEntry->d_name);
}

'DT_REG' should be defined in 'dirent.h'


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

mrb28Author Commented:
what is DT_REG and d_type.. i get compilation errors when i use the code. where do i find the structure of DIR ?

line 234: Error: DT_REG is not defined.
line 234: Error: d_type is not a member of DIR.
Error: d_name is not a member of DIR.
line 239: Error: d_name is not a member of DIR.
4 Error(s) detected.

d_name maybe be member of dirent* pEntry instead.
0
mrb28Author Commented:
thanks jkr that helped a lot..
0
jkrCommented:
Hum, does it work?
0
mrb28Author Commented:
does work with some modifications
0
jkrCommented:
So, could we close this Q then? :o)
0
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to jkr: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.