Why doesn't this work ?

Can someone help me out ? I want to have a script which deletes
files in a certain directory on the server, that are older than let's
say 30 day's. I already have some code but it doesn't do the job.

Thanks in advance,
Mike

#!/usr/local/bin/perl
$PAD='/dummy/dummy/dummy/';
$DAYS=30;
$LIMIET=$DAYS*86400;
### 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;
 }
}
print "Content-type: text/plain\n\n";
print "Old Files Deleted.\n";
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:
It sounds like the file permissions on the files to be deleted may not be set properly.  Make sure that the user that your CGI script gets run as (it usually isn't you) has sufficient permissions to delete the files.  This means that the CGI user should either own the files or the files should be writtable by all users.  The same is true for the directory in which the files reside.

You might want to make your program print out the names of the files that it encounters which match its date criteria.  Assumming that the right file names are displayed, file permissions must be the problem.
0
ozoCommented:
opendir(DIR, $PAD) or print "Content-type: text/plain\n\ncan't opendir $PAD because $! ";
@files=readdir(DIR);
closedir DIR;

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

if ($mtime < time - $LIMIET) {
    unlink $PAD.$filename or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! ";
 }
}
0
oostwijkAuthor Commented:
Ok, Ozo. I get the error message :
can't opendir /dummy/dummy/ because No such file or directory. Though I know for sure that the directory i typed in is good.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

mattropeCommented:
Just want to verify:

/dummy/dummy is an absolute directory name, not a relative directory (i.e.), the entire path name is /dummy/dummy, not /more/path/stuff/dummy/dummy ?
0
oostwijkAuthor Commented:
It's a relative directory :
'/path/dummy/';

Hope that you can help me out here.
0
oostwijkAuthor Commented:
It's a relative directory :
'/path/dummy/';

Hope that you can help me out here.
0
mattropeCommented:
In that case, take off the leading /

The leading slash makes it an absolute path.
0
oostwijkAuthor Commented:
I already tried that but it's still giving the message:
can't opendir path/dummy/

 
0
ozoCommented:
What is the full path to the dummy directory?
What is the working directory of the running script?
0
oostwijkAuthor Commented:
working directory:
#!/usr/local/bin/perl
full path:
servername/username/dummy/
0
mattropeCommented:
By working directory, he means in what directory does your script reside?

/usr/local/bin/perl is where perl resides, not your script.
0
oostwijkAuthor Commented:
Ok, the working directory is:
servername/username/cgi-bin/scriptfile
and the files that must be deleted are in:
servername/username/dummy/
0
mattropeCommented:
Okay, what you need to do is specify the directory relative to the script's directory:

.../dummy

As an alternative, you could also use the entire absolute directory path.
0
oostwijkAuthor Commented:
I still keep getting the same error message, please help me out.
0
mattropeCommented:
It looks like I mistyped my comment...there should only be two periods before the slash:

.../dummy

Also, change

unlink $PAD.$filename or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! ";

to

$curdir = `pwd`;
unlink $PAD.$filename or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! (running from $curdir)";

This will verify that the script is running in the directory you think it is.  Make sure that the ticks around the pwd are the ones on the ~ key.
0
oostwijkAuthor Commented:
Ok, there is some progress in it. Now I'm getting the error message:
can't unlink ../test. because No such file or directory (running from pwd)Content-type: text/plain can't unlink ../test.. because No such file or directory (running from pwd)Content-type: text/plain can't unlink ../testDELETEIT.BAK because No such file or directory (running from pwd)Content-type: text/plain can't unlink ../testImage36.jpg because No such file or directory (running from pwd)

I do hope that you know how to solve it.
Thanks !!!!!!
0
mattropeCommented:
Okay, change

unlink $PAD.$filename or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! ";

to

if ($filename != /^\.+$/) {
  unlink "$PAD/$filename" or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! ";
}


It appears that your pathname and filename were getting run together without a '/' in between.  This will also keep it from trying to delete the directories "." and ".."
0
ozoCommented:
Yes, originally $PAD contained a training / so there was no need for another one in front of $filename.
Now that $PAD no longer ends in / you have to add it in "$PAD/$filename"

I'd have used instead
  if( -f "$PAD/$filename" )
since some non-direcories can be named with dots, and some directories can be named without dots
0
oostwijkAuthor Commented:
Ok, now I'm getting a 500 errormessage, somethings wrong with the script, but I can't find the problem.

#!/usr/local/bin/perl
$PAD='../test';
$DAYS=1;
$LIMIET=$DAYS*86400;
### Get a list of files in the directory
opendir(DIR, $PAD) or print "Content-type: text/plain\n\ncan't opendir $PAD because $! ";
@files=readdir(DIR);
closedir DIR;

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

if ($mtime < time - $LIMIET) {
    $curdir = 'pwd';
if ($filename != /^\.+$/) {
  unlink "$PAD/$filename" or print "Content-type: text/plain\n\ncan't unlink $PAD$filename because $! ";
  }
 }
}
0
mattropeCommented:
Okay, I think we're back to the problem with it not producing output...put

print "Content-type: text/plain\n\n";
print "Old files deleted.\n";

after everything else and see if the error persists.
0
ozoCommented:
Why don't we just
  print "Content-type: text/plain\n\n";
right at the top, so we don't need to keep repeating it for every possibility.
0
oostwijkAuthor Commented:
Ok, I don't receive an error message any more. The script runs all the way, but the files which have to be deleted aren't removed.
0
mattropeCommented:
You don't get any message from the unlink statement?  Right before your unlink statement, try putting the following line in:

print "About to delete $PAD/$filename\n";

This should produce a list of the files it thinks should be deleted, based on date.  Make sure that it *is* finding files to delete properly.
0
ozoCommented:
I notice that you didn't change
stat($PAD.$filename)
to
stat "$PAD/$filename"

or an easier test might be
if( -M "$PAD/$filename" > $DAYS )
0
oostwijkAuthor Commented:
Right now the script looks like this:

#!/usr/local/bin/perl
print "Content-type: text/plain\n\n";
$PAD='../test';
$DAYS=1;
$LIMIET=$DAYS*86400;
### Get a list of files in the directory
opendir(DIR, $PAD) or print "Can't opendir $PAD because $! ";
@files=readdir(DIR);
closedir DIR;

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

if ($mtime < time - $LIMIET) {
    $curdir = 'pwd';
if ($filename != /^\.+$/) {
  print "About to delete $PAD/$filename\n";
  unlink "$PAD/$filename" or print "Can't unlink $PAD$filename because $! ";
  }
 }
}
print "Old files deleted.\n";

I don't get any message at my screen and the files aren't deleted


0
oostwijkAuthor Commented:
I'm going away this weekend so I won't be able to response to you the next 2 day's. I do hope you can solve my problem.

Have a nice weekend...
0
oostwijkAuthor Commented:
I'm going away this weekend so I won't be able to response to you the next 2 day's. I do hope you can solve my problem.

Have a nice weekend...
0
ozoCommented:
(stat($PAD/$filename))[9];
should be
(stat("$PAD/$filename"))[9];

if ($filename != /^\.+$/)
should be
if ($filename !~ /^\.+$/)

although I still think
if( -f "$PAD/$filename" )
would be preferable
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:
Ok, the script finally works, thank you very much !!!
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.