?
Solved

Not the required output.

Posted on 2000-03-02
11
Medium Priority
?
201 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
[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
  • 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
Industry Leaders: 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: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
 
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 150 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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