> -----Original Message-----
> From: Shepard, Gregory R [mailto:[EMAIL PROTECTED]] 
> Sent: Wednesday, July 25, 2001 10:56 AM
> To: '[EMAIL PROTECTED]'
> Subject: stat() keeps returningthe incorrect date
> 
> 
> I am trying to create a script which gathers the stat info of 
> every file in a given directory. Then return the date it was 
> last modified for the purpose of picking the file that was 
> created/modiefied last. The script is reading the files ok, 
> however, when placing them in the stat() to exctact the info, 
> it only reads the . and .. files correctly, while returning 
> the same date for all of the text files. The incorrect date I 
> get for them is: Last modified at:Wed Dec 31 18:00:00 1968

No, . and .. aren't really working correctly. See below.

> 
>       @ARGV=$dir;
>       $directory=shift || '.';                
>       opendir DIR, $directory || die "Can't open directory 
> $directory: $!\n";
>       
>       while ($file=readdir DIR) 
>               {
>               #print "$file\n";
>               @filespecs=stat("$file");

The problem is here. You need to check the return of stat(). $file is the
filename relative to the directory in $directory. But you are trying to
stat() it relative to the current working directory, whatever that is.
stat() is returning an empty list, since the file doesn't exist.

>               $last_modification_time = localtime($filespecs[9]);

$filespecs[9] is undef, which is treated as a time value of 0, which is
giving you the 1969 date.

>               print "Last modified at:$last_modification_time\n";
>               #print "$mytime\n";
>               }
> 
> How do I get it to read the date it was last created/modified?

Solution is to append the file name to the directory to get a full path
before calling stat().

Using -w in your script would have given you a warning that you were using
an undefined value on the localtime call.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to