CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Deleting Old Files

 



jr
Novice

Apr 3, 2000, 12:39 PM

Post #1 of 4 (745 views)
Deleting Old Files Can't Post

I have a directory that stores temporary images prior to a full posting of information, I wrote a script that should delete any images older than one day, but it isn't doing it. I can't even get it to spit an error message out to me...

Since I am currently doped up on cold medicine, I don't think my brain is working correctly, if at all...so any help would be apreciated:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub image_temp_clean {
$imagetempdays = 1;
opendir THEDIR, "$imagetempdir" &#0124; &#0124; die "Unable to open directory: $!";
@allimage = readdir THEDIR;
closedir THEDIR;
foreach $file (sort { int($b) <=> int($a) } @allimage) {
if (-T "$imagetempdir/$file") {
$imagetempdir2 = "$imagetemp/$file";
($imagedate) = (stat($imagetempdir2))[9];
$imagedate =~ s/\D//g;
$currenttime = time;
$comparetime = ($currenttime - ($imagetempdays * 86400));
if ($comparetime > $imagedate) {
unlink "imagetempdir/$file" if (($file ne "index.html") && ($file ne ".htaccess"));
}
}
}
}
</pre><HR></BLOCKQUOTE>

Thanks in advance


japhy
Enthusiast

Apr 3, 2000, 1:55 PM

Post #2 of 4 (745 views)
Re: Deleting Old Files [In reply to] Can't Post

You're not getting any errors because you're not asking for them. You're missing the $ in the unlink() function, in front of the variable name. That's your problem. To get an error message from Perl (that you want), check unlink()'s return value:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


unlink $foobar or warn "can't delete $foobar: $!";
</pre><HR></BLOCKQUOTE>


jr
Novice

Apr 3, 2000, 3:15 PM

Post #3 of 4 (745 views)
Re: Deleting Old Files [In reply to] Can't Post

Okay, I see that now and I fixed it...and it works...almost.

It only deletes my .htaccess and index.html files, not the other files in the directory even though I specified the unlink command only if $file is not equal to index.html or .htaccess.

I have got to be missing something trivial here...

Oh, and I tried the same coding with an UNLESS command rather than an IF (for the index and htaccess) as well as trying to remove the file pre-requisites completely. The first way did still deleted the two files and none others while the second option did nothing at all...


japhy
Enthusiast

Apr 3, 2000, 4:21 PM

Post #4 of 4 (745 views)
Re: Deleting Old Files [In reply to] Can't Post

Hmm... first, I don't see a need for you to sort the array. You can make this program a bit nicer by doing the following:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$dir = "whatever/path";
$days = 1;
opendir DIR, $dir or die "can't open $dir: $!";
while (defined ($file = readdir DIR)) {
next if $file eq "index.html" or $file eq ".htaccess";
$modtime = (stat "$dir/$file")[9];
if ($modtime > ($^T - $days * 86400)) {
warn "attempting to remove $dir/$file: $!";
unlink "$dir/$file" or warn "can't remove $dir/$file: $!";
}
}
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives