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:
Problem getting file attributes

 



Maebius
Novice

Dec 12, 2000, 10:55 AM

Post #1 of 3 (235 views)
Problem getting file attributes Can't Post

Greetings,
I am working on a postcard-style script, and am having some troubles with getting the file attributes of the generated data files.

When a new postcard/message is created, it generates a randomly-named file in the path "..cgi-bin/postcards/12345.dat"
Here is the code I have to check the files and (hopefully) delete the older files. If I modify the
@dir= grep !/^\./, readdir(DIR);
line to NOT filter out the directories, those appear in the output and actually have values in $filedate instead of a null value. Hmmm?

Code
$path = "postcards/"; 
$now = time(); #Get current time (epoc secs)
$day = 60*60*24; #seconds in a day
$cutoff = $now - ($day * 5); #cutoff is 1 days

print "The time is: $now";
print "
Day is: $day";
print "
The Cutoff is: $cutoff";

opendir(DIR, "$path") or die "$!";
@dir= grep !/^\./, readdir(DIR);

foreach $file (@dir){
$filedate = (-C "$file"); #check creation date
print "
The file to be deleted is: $file";
print "
The age of the file is: $filedate";
print "<p>\n";
if ($filedate > $cutoff) {
#unlink ("$file");
}
}
closedir(DIR);


When I run this, it generated the output as follows:
The time is: 976640896
Day is: 86400
The Cutoff is: 976208896
The file to be deleted is: 92500.dat
The age of the file is:

The file to be deleted is: 82301.dat
The age of the file is:

The file to be deleted is: 24502.dat
The age of the file is:



any thoughts or advice? Oroginally, I was going to use the last modified date (stat $file)[9], but that was returning null values for $filedate as well. Using the create date (-C $file) is more what I wanted to check anyway. I can't figure out where I might be going wrong.





Rivotti
User

Dec 12, 2000, 11:08 AM

Post #2 of 3 (234 views)
Re: Problem getting file attributes [In reply to] Can't Post

Hi Maebius

Instead of using:
$filedate = (-C "$file");

use:
$filedate = -C "$file";

But I think this will resolve the problem...
$filedate = -C "postcards/$file";

the (-C "$file") do the same as -C "$file" but sometimes things are just ilogic, dont they? So you dont loose nothing trying...

Rivotti

(This post was edited by Rivotti on Dec 12, 2000, 10:14 AM)


Maebius
Novice

Dec 12, 2000, 11:40 AM

Post #3 of 3 (232 views)
Re: Problem getting file attributes [In reply to] Can't Post

Silly me!

You were correct. The only thing I was mising was the actual path! Now everything works correctly (and is shorter and more efficient).

Here's the final working routine:

$path = 'postcards/'
opendir(DIR, "$path") or die "$!";
@dir= grep !/^\./, readdir(DIR);
foreach $file (@dir){
$filedate = -C "$path$file";
if ($filedate > 60) {
unlink ("$path$file");
}
}
closedir(DIR);


living life -umop apisdn-
maebius@everthorn.net

 
 


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

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