• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Not the required output.

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
begin
Asked:
begin
  • 3
  • 3
  • 3
  • +1
1 Solution
 
alexoCommented:
The directory "." (aka "the current directory") is getting processed.  You probably should treat the "." entry (and maybe the ".." entry too) differently.
0
 
nietodCommented:
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
 
beginAuthor Commented:
Adjusted points to 50
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
beginAuthor Commented:
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
 
nietodCommented:
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
 
RONSLOWCommented:
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
 
alexoCommented:
>>  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
 
beginAuthor Commented:
>>>   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
 
RONSLOWCommented:
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
 
alexoCommented:
>> guess if you want to start directory names with '.' that is fair enough.

Common practice on unix platforms.
0
 
RONSLOWCommented:
eewww .. unix :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now