Solved

Not the required output.

Posted on 2000-03-02
11
191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 291
SetCurrentDirectory path limit 7 105
Excel/Word Add-in in what language? 5 107
Dynamically allocate memory 9 57
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

867 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

18 Experts available now in Live!

Get 1:1 Help Now