?
Solved

Why doesn't this work ?

Posted on 2000-01-26
29
Medium Priority
?
171 Views
Last Modified: 2013-12-25
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";
0
Comment
Question by:oostwijk
  • 14
  • 9
  • 6
29 Comments
 
LVL 2

Expert Comment

by:mattrope
ID: 2391053
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
 
LVL 85

Expert Comment

by:ozo
ID: 2391429
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
 

Author Comment

by:oostwijk
ID: 2391803
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 2

Expert Comment

by:mattrope
ID: 2391851
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
 

Author Comment

by:oostwijk
ID: 2393191
It's a relative directory :
'/path/dummy/';

Hope that you can help me out here.
0
 

Author Comment

by:oostwijk
ID: 2393198
It's a relative directory :
'/path/dummy/';

Hope that you can help me out here.
0
 
LVL 2

Expert Comment

by:mattrope
ID: 2393333
In that case, take off the leading /

The leading slash makes it an absolute path.
0
 

Author Comment

by:oostwijk
ID: 2393421
I already tried that but it's still giving the message:
can't opendir path/dummy/

 
0
 
LVL 85

Expert Comment

by:ozo
ID: 2393562
What is the full path to the dummy directory?
What is the working directory of the running script?
0
 

Author Comment

by:oostwijk
ID: 2393662
working directory:
#!/usr/local/bin/perl
full path:
servername/username/dummy/
0
 
LVL 2

Expert Comment

by:mattrope
ID: 2393703
By working directory, he means in what directory does your script reside?

/usr/local/bin/perl is where perl resides, not your script.
0
 

Author Comment

by:oostwijk
ID: 2394041
Ok, the working directory is:
servername/username/cgi-bin/scriptfile
and the files that must be deleted are in:
servername/username/dummy/
0
 
LVL 2

Expert Comment

by:mattrope
ID: 2394193
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
 

Author Comment

by:oostwijk
ID: 2394554
I still keep getting the same error message, please help me out.
0
 
LVL 2

Expert Comment

by:mattrope
ID: 2394759
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
 

Author Comment

by:oostwijk
ID: 2394830
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
 
LVL 2

Expert Comment

by:mattrope
ID: 2394865
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
 
LVL 85

Expert Comment

by:ozo
ID: 2394905
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
 

Author Comment

by:oostwijk
ID: 2394919
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
 
LVL 2

Expert Comment

by:mattrope
ID: 2394936
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
 
LVL 85

Expert Comment

by:ozo
ID: 2394956
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
 

Author Comment

by:oostwijk
ID: 2395644
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
 
LVL 2

Expert Comment

by:mattrope
ID: 2395650
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
 
LVL 85

Expert Comment

by:ozo
ID: 2395798
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
 

Author Comment

by:oostwijk
ID: 2397463
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
 

Author Comment

by:oostwijk
ID: 2397686
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
 

Author Comment

by:oostwijk
ID: 2397698
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
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 2397843
(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
 

Author Comment

by:oostwijk
ID: 2447702
Ok, the script finally works, thank you very much !!!
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

598 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