Solved

Not the required output.

Posted on 2000-03-02
11
188 Views
Last Modified: 2010-04-02
Hi, please look into piece of a code,

------
....
....
int Inp_time = mktime(&tm_str) ;

  DIR* Dir_path = opendir("tt") ;
  dirent* Dir_ptr = readdir(Dir_path) ;
  while (Dir_ptr != NULL)
  {
    stat(Dir_ptr->d_name, &Stat) ;
    int filetime = Stat.st_ctime ;
    if ( Inp_time > filetime )
    {
      printf("The file %s is being deleted \n ",Dir_ptr->d_name) ;
      unlink(Dir_ptr->d_name) ;
    }
  }
  closedir(Dir_path) ;
....
....
-----
What i supposed to do in the above piece of code is that by checking a directory "tt" in the current directory for files which are older than those specified by the user as input, in this recursive checking, files which are older are deleted. When i run this, the files to be are not deleted. Instead i get the below output on execution.

output
The file . is being deleted
 The file . is being deleted
 The file . is being deleted
 The file . is being deleted
 The file . is being deleted
 The file . is being deleted
..... goes on ....

Kindly help me in this situation, hope you got the question.
0
Comment
Question by:begin
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 2577397
The directory "." (aka "the current directory") is getting processed.  You probably should treat the "." entry (and maybe the ".." entry too) differently.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2577580
both "." and ".." are directories, you probably don't want to be deleting directoryes, and (certainly not these two) so you should skip any directories.  You should be able to determine which entries are directories from info returned in the stat structure.
0
 

Author Comment

by:begin
ID: 2577685
Adjusted points to 50
0
 

Author Comment

by:begin
ID: 2577686
In the mentioned direc. there might be several sub-directories, which also might be older, so the directory and the files it contain must be deleted.
--gsbabu
0
 
LVL 22

Expert Comment

by:nietod
ID: 2577819
then delete those directories too.  Just don't try to delete "." and ".."  That IS the answer, I don't see why you increased the point value.  You just don't want to delete these two directories.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2578917
Note that with the code above, it is going to loop forever because it never changes Dir_ptr .. once it is not null it will remain not null forever and try to delete the same file over and over

I am assuming this readdir function reads entries from the directory one at a time, so you need to call readdir in each loop. (correct me if I am wrong .. I don't have any info on the opendir or readdir functions).

And, as pointer out, it will try to kill the directory itself and the parent directory (..), so I've stoped it doing that.

One thing you'll need to check is whether the readdir will return something sensible after a file deletion .. I assum it does).

Anyway, try this...

  DIR* Dir_path = opendir("tt") ;
  while (Dir_ptr != NULL)
  {
    dirent* Dir_ptr = readdir(Dir_path) ; // <-- move to here
    stat(Dir_ptr->d_name, &Stat) ;
    if (Dir_prt->d_name[0] == '.') continue; // <-- add this
    int filetime = Stat.st_ctime ;
    if ( Inp_time > filetime )
    {
      printf("The file %s is being deleted \n ",Dir_ptr->d_name) ;
      unlink(Dir_ptr->d_name) ;
    }
  }
  closedir(Dir_path) ;
.....

0
 
LVL 11

Expert Comment

by:alexo
ID: 2579746
>>  if (Dir_prt->d_name[0] == '.') continue; // <-- add this

Don't add this.  It will skip all directories that start with a period - definitely not what you want.  Just strcmp() with "." and with ".."
0
 

Author Comment

by:begin
ID: 2580579
>>>   dirent* Dir_ptr = readdir(Dir_path) ; // <-- move to here
-i think we cannot move the above line into the loop as we have not previously assigned Dir_ptr.

let me once again clearly put the question.

In my current directory, i have several sub-directories, in which you have numerous files. What i want to do is that from my current working directory i need to check all (in loop) directories/files under it whether its date is older than what specified as input. if it is i want to remove it.
 
Kindly send in your comments.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 2585991
alexo ..
>>  if (Dir_prt->d_name[0] == '.') continue; // <-- add this
>Don't add this.  It will skip all directories that start with a period -
>definitely not what you want. Just strcmp() with "." and with ".."

guess if you want to start directory names with '.' that is fair enough.

So, add:
if (0 == strcmp(Dir_ptr->d_name,".") || 0 == strcmp(Dir_ptr->d_name,"..")) continue;

begin ..
>>>>   dirent* Dir_ptr = readdir(Dir_path) ; // <-- move to here
>-i think we cannot move the above line into the loop as we have not previously
>assigned Dir_ptr.

That is not a problem.  The Dir_path is opened outside the loop.  I assume readdir just returns the dirent for the next file in the directory.  In hwich case this is EXACTLY what you want to do .. get the next file in the directory in each loop.

As I said .. I have no docs on readdir and opendir (not part of Windows, standard C/C++ etc).  But on what I do know aoubt them, my answer seemed to make sense.

Did you try it?


0
 
LVL 11

Expert Comment

by:alexo
ID: 2592340
>> guess if you want to start directory names with '.' that is fair enough.

Common practice on unix platforms.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2593551
eewww .. unix :-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now