Gossamer Forum
Home : General : Perl Programming :

File time

Quote Reply
File time
What code would I use to return the date and time a file (user.dat) was created, as in:

This user joined us on November 6, 2003 1:51 PM CST



thanks in advance

Paul
Quote Reply
Re: [ceo] File time In reply to
Hi Paul,

This should help: http://www.perldoc.com/...s-timestamp-in-perl-

~Charlie
Quote Reply
Re: [Chaz] File time In reply to
Blush Thanks but I'm a bit of a newbie..this is a bit over my head....
Quote Reply
Re: [ceo] File time In reply to
Sorry about that Paul. See if this helps:

Code:
[17:30][piper@galaxy:~]$ perl -e '
> my $unix_time = (stat(".viminfo"))[9];
> my $pretty_time = localtime($unix_time);
> print "\n\nUnix time: $unix_time - Pretty time: $pretty_time\n\n";'


Unix time: 1068093465 - Pretty time: Wed Nov 5 23:37:45 2003

[17:30][piper@galaxy:~]$

The stat function returns an array of info about the file. By using (stat('file.ext'))[9], you are asking it for the 10th element of that array which holds the unix timestamp of the file. localtime($unix_time) just returns a string with a nicely formatted time as you can see above. You can do a search at http://www.perldoc.com for more specific info on stat and localtime. I hope that clears it up a little.

~Charlie
Quote Reply
Re: [Chaz] File time In reply to
Blush Where do I place this in the script. I just get a date of Jan 1, 1969 etc
File name is *.dat

#!/usr/bin/perl
my $path = '/..../..../...../nm';
print "Content-type: text/html\n\n";
opendir NEW, $path or die $!;
my @files = sort { -M "$path/$a" <=> -M "path/$b" } readdir NEW;
closedir NEW;
my $latest = $files[-1];
open MEMBER, "$path/$latest" or die $!;
my $alias = <MEMBER>;
close MEMBER;
chomp $alias;
print "<font face=\"verdana,Arial\" color=\"black\" size=\"1\">Welcome to a new member<br><b>$alias</b></font>";



thanks again
Paul
Quote Reply
Re: [ceo] File time In reply to
Hi Paul,

Is this what you're after?

Code:
my $path = '/..../..../...../nm';
print "Content-type: text/html\n\n";
opendir NEW, $path or die $!;
my @files = sort { (stat("$path/$b"))[9] <=> (stat("$path/$a"))[9] } readdir NEW;
closedir NEW;
my $latest = shift @files;
open MEMBER, "$path/$latest" or die $!;
my $alias = <MEMBER>;
close MEMBER;
chomp $alias;
my $file_time = localtime((stat("$path/$latest"))[9]);
print "<font face=\"verdana,Arial\" color=\"black\" size=\"1\">Welcome to a new member<br><b>$alias($files[0])</b></font> Joined on $file_time.";

~Charlie
Quote Reply
Re: [Chaz] File time In reply to
Smile thanks....