Gossamer Forum
Home : General : Perl Programming :

File Time

Quote Reply
File Time
How can I read the time a file was written into a variable?

Lets say for example I want to read in all the times my session files were written

opendir (DIR, $path) or die "Can't read sessions : $path : $!";

@fiiletimes = ????????? readdir(DIR);
closedir DIR;



Thanks!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] File Time In reply to
Hi

Unfortunately, if you're on a *ix box, there's no such thing as timestamp of when a file was created. It only records when the file was last modified. If this is any use to you then you should look at perldoc -f stat for ways of gathering information about a file's last modified date, size, location and so on.

- wil
Quote Reply
Re: [Wil] File Time In reply to
Hi Wil, yes that is what I want. Thanks!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] File Time In reply to
OK. Then check out perldoc -f stat which is also available for you online at:

http://www.perldoc.com/...1/pod/func/stat.html

If you need any further guidance implementing this into your code, just ask!

Hope this helps.

- wil
Quote Reply
Re: [Wil] File Time In reply to
Thanks Wil, I found the reference, read it, and scratched my head a little. I am very amature at PerlSmile

This is for inclusion in the whos online script at: http://www.gossamer-threads.com/...orum.cgi?post=196951

Which has:

my $path = "/s/mysite.com/cgi-bin/admin/sessions";

opendir (DIR, $path) or die "Can't read sessions : $path : $!";
@sess = grep { /^\w+$/ } readdir(DIR);
closedir DIR;

for reading in the session files (from a script Paul wrote).

I just want the a time associated with the session file (modified or created, does not really matter) to print out with the @sess (which shows the username).

The links session files (/admin/session) are created each time someone logs in (thats my understanding).

So I am not sure if you are following my ramble here, but a little bit of perl code to show me how to read in the time form these session files would be great!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] File Time In reply to
Well, from the snippet you posted, I would use something like:

Code:
opendir (DIR, $path) or die "Can't read sessions : $path : $!";
@sess = grep { /^\w+$/ } readdir(DIR);
closedir DIR;

foreach my $sess (@sess) {
my $file = $path . "/" . $sess;
my $modtime = localtime( (stat($file))[9] );
# $modtime now holds the modified time of the current file in your loop.
}

Does this help you at all? I'm not sure exactly what you and Paul are trying to do with your Who's Online script, and I have no working knowledge of Links SQL so I can't really help you with implementing this into Links SQL without further information from you and possibly some more code sample.

- wil

Last edited by:

Wil: May 17, 2002, 4:37 PM
Quote Reply
Re: [Wil] File Time In reply to
Or:

my $modtime = -M "$path/$sess";

Last edited by:

Paul: May 17, 2002, 4:07 PM
Quote Reply
Re: [Paul] File Time In reply to
Yeah. I believe we've had this discussion before:

http://www.gossamer-threads.com/...orum.cgi?post=175795

- wil
Quote Reply
Re: [Wil] File Time In reply to
Thanks Both!!Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] File Time In reply to
I don't see -M mentioned in that thread.
Quote Reply
Re: [Paul] File Time In reply to
True. I was more making a point of TMTOWTDI.

Anyway, it's late here and I'm going for another glass of wine. My stocks did well today:

http://finance.yahoo.com/q?s=tyc&d=v1

:-)

- wil
Quote Reply
Re: [Paul] File Time In reply to
Maybe I have done something wrong, but

this: my $modtime = localtime( (stat($sess))[9] );

gives me: (Wed Dec 31 19:00:00 1969) weird time? same for each file also.

and this: my $modtime = -M "$path/$sess";

gives me: () blank


Edit: regarding the above posts between you guys... should I change this to:

my $modtime = strftime('%Y-%m-%d', localtime($sess->mtime)); ?


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

sooke: May 17, 2002, 4:36 PM
Quote Reply
Re: [sooke] File Time In reply to
What's the output of the date command on your system?

- wil
Quote Reply
Re: [sooke] File Time In reply to
Wils code won't work because the full path isn't specified.

My code should work.
Quote Reply
Re: [Paul] File Time In reply to
Yup. I've now modified the following post which should work:

http://www.gossamer-threads.com/...i?post=196954#196954

- wil
Quote Reply
Re: [Wil] File Time In reply to
Well I dont have shell acess, but an example of one of the session files associated date is:

Modified: 05/17/02 06:28 when I check it with an FTP prog.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] File Time In reply to
Ok tried:

my $file = $path . "/" . $sess;
my $modtime = localtime( (stat($file))[9] );




and still get (Wed Dec 31 19:00:00 1969)


Is this something to do with my host's server then?


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] File Time In reply to
Can you post your code?

- wil
Quote Reply
Re: [Paul] File Time In reply to
Yes Paul, I am sure your code will work.... but It is just giving me a blank!

The funny things is it can find the session file no prob, but just can't read its date?


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] File Time In reply to
Yes!


Attachement Deleted.

I'll post it in the propert area with my plugin when its done.

Thanks guys.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

sooke: May 17, 2002, 4:59 PM
Quote Reply
Re: [sooke] File Time In reply to
Try changing:

my $file = $path . "/" . $sess;

to

my $file = $path . "/" . $_;

Smile
Quote Reply
Re: [sooke] File Time In reply to
From my initial reading of your code, I can see we've used a bad variable name (one that is already in use somewhere else). Try changing the following lines:

From:
my $file = $path . "/" . $sess;

To:
my $file = $path . "/" . $_;

- wil
Quote Reply
Re: [Paul] File Time In reply to
Thats it!!!!!

Thanks both of you!

Cool


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] File Time In reply to
Its nice when the experts come up with the same answer! (sometimes)Tongue


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] File Time In reply to
chop('experts');

Cool j/k