oostwijk
asked on
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";
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";
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 $! ";
}
}
@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 $! ";
}
}
ASKER
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.
can't opendir /dummy/dummy/ because No such file or directory. Though I know for sure that the directory i typed in is good.
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/dum my ?
/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/dum
ASKER
It's a relative directory :
'/path/dummy/';
Hope that you can help me out here.
'/path/dummy/';
Hope that you can help me out here.
ASKER
It's a relative directory :
'/path/dummy/';
Hope that you can help me out here.
'/path/dummy/';
Hope that you can help me out here.
In that case, take off the leading /
The leading slash makes it an absolute path.
The leading slash makes it an absolute path.
ASKER
I already tried that but it's still giving the message:
can't opendir path/dummy/
can't opendir path/dummy/
What is the full path to the dummy directory?
What is the working directory of the running script?
What is the working directory of the running script?
ASKER
working directory:
#!/usr/local/bin/perl
full path:
servername/username/dummy/
#!/usr/local/bin/perl
full path:
servername/username/dummy/
By working directory, he means in what directory does your script reside?
/usr/local/bin/perl is where perl resides, not your script.
/usr/local/bin/perl is where perl resides, not your script.
ASKER
Ok, the working directory is:
servername/username/cgi-bi n/scriptfi le
and the files that must be deleted are in:
servername/username/dummy/
servername/username/cgi-bi
and the files that must be deleted are in:
servername/username/dummy/
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.
.../dummy
As an alternative, you could also use the entire absolute directory path.
ASKER
I still keep getting the same error message, please help me out.
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.
.../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.
ASKER
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 !!!!!!
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 !!!!!!
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 ".."
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 ".."
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
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
ASKER
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 $! ";
}
}
}
#!/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 $! ";
}
}
}
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.
print "Content-type: text/plain\n\n";
print "Old files deleted.\n";
after everything else and see if the error persists.
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.
print "Content-type: text/plain\n\n";
right at the top, so we don't need to keep repeating it for every possibility.
ASKER
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.
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.
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.
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 )
stat($PAD.$filename)
to
stat "$PAD/$filename"
or an easier test might be
if( -M "$PAD/$filename" > $DAYS )
ASKER
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
#!/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
ASKER
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...
Have a nice weekend...
ASKER
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...
Have a nice weekend...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, the script finally works, thank you very much !!!
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.