Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find files depending on substr and delete them??

Status
Not open for further replies.

kaio123

Programmer
Feb 27, 2003
3
US
I am 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};


time_ptr =localtime(&lt);
cur_year = time_ptr->tm_year +1900;
cur_month = time_ptr->tm_mon +1;
cur_day =time_ptr->tm_mday;



/*
//find all files matching substr
//get the name of those files
if (remove(files)!= 0){
cout <<&quot;cant removed file \n&quot;;
}
*/
string rmstr(&quot; rm &quot;);
string filestr(&quot;ABU030227*&quot;);
string remove_cmd = rmstr + filestr;
cout <<&quot;the STRING IS =&quot;<<remove_cmd<<endl;
if(system(remove_cmd.c_str())== -1){
cout <<&quot;cant removed file \n&quot;;
}
}

 
Dirty way to do it....


Code:
string rmstring;
//get the string value
system( (&quot;ls *&quot; + rmstring + &quot;* > temp&quot;).c_str());
fstream fin;
fin.open(&quot;temp&quot;);
while(fin >> rmstring)
{
   system( (&quot;rm &quot; + rmstring).c_str());
}


I didn't test this code, it's not the best way to solve the problem, and it depends on several calls to utilities inside the OS.... It is O(n) and should get the job done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top