Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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";
 }
}
0
mrb28
Asked:
mrb28
  • 4
  • 4
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now