?
Solved

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

Posted on 2003-02-28
10
Medium Priority
?
171 Views
Last Modified: 2012-06-27
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
Comment
Question by:mrb28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8043902
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
 

Author Comment

by:mrb28
ID: 8044401
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
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8044499
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mrb28
ID: 8044918
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
 

Author Comment

by:mrb28
ID: 8044922
thanks jkr that helped a lot..
0
 
LVL 86

Expert Comment

by:jkr
ID: 8044972
Hum, does it work?
0
 

Author Comment

by:mrb28
ID: 8082449
does work with some modifications
0
 
LVL 86

Expert Comment

by:jkr
ID: 8082482
So, could we close this Q then? :o)
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9887926
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question