Deleting a file

Hi there,

Can someone help me out ? I wan't to have a script which deletes
files in a certain directory on the server, that are older than let's
say 30 day's. Is there anybody out there who can tell me how I can do this ?

Thanks in advance,
Mike

oostwijkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mattropeCommented:
You can check the file's modification date with the "stat" command, and then determine whether you want to delete it or not.

### Get a list of files in the directory
opendir(DIR, $dir_name);
@files = readdir(DIR);
closedir DIR;

### Check each file and delete if necessary

foreach $filename (@files) {
  $mtime = (stat($filename))[9];

  if ($mtime < time - $TIME_LIMIT) {
     unlink $filename;
  }
}



Obviously this assumes that your CGI script runs with sufficient permissions to list the directory contents and delete files in the directory.  $TIME_LIMIT is how old the file must be to be deleted, in seconds.  You can convert this into days with something like $NUMDAYS * 24 * 60 * 60.

Hope this is what you were looking for.
0
oostwijkAuthor Commented:
Mattrope, I keep getting a [500]Error message. What's wrong with the code ?
If you can help me run the script, i'll give the points to you.

#!/usr/local/bin/perl
$PAD='dummy/dummy/dummy/';
$NUMDAYS=1;
$LIMIET=$NUMDAYS*24*60*60;
### Get a list of files in the directory
opendir(DIR, $PAD);
@files=readdir(DIR);
closedir DIR;

### Check each file and delete if necessary
foreach $filename (@files) {
 $mtime=(stat($filename))[9];

 if ($mtime<time - $LIMIET) {
    unlink $filename;
 }
}
0
oostwijkAuthor Commented:
Adjusted points to 155
0
mattropeCommented:
The error may be that your script produces no output page after deleting the files.

At the end, put something like:

print "Content-type: text/plain\n\n";
print "Old Files Deleted.\n";

See if the error continues...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oostwijkAuthor Commented:
Great Job, keep up the good work !!

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.