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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Deleting Old Files



Apr 3, 2000, 12:39 PM

Post #1 of 4 (1698 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 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"));

Thanks in advance


Apr 3, 2000, 1:55 PM

Post #2 of 4 (1698 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: $!";


Apr 3, 2000, 3:15 PM

Post #3 of 4 (1698 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...


Apr 3, 2000, 4:21 PM

Post #4 of 4 (1698 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: $!";


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

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