For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Re: Invalid data-time stamps









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Invalid data-time stamps
John W. Krahn

2005-03-07, 8:56 pm

Bret Goodfellow wrote:
> Hi all,


Hello,

> I am attempting to list out the contents of a directory, and print the
> last time accessed. The data-time is not correct though. I am running
> this code on a Windows XP workstation. There doesn't seem to be a real
> easy way to get the timestamps of directories. Hmmmm.... Here is the
> code:
>
> # deldir4.pl
> use strict;
> # use warnings;
> use File::Find;
>
> my $DIRHANDLE;
> my $file;
> my $write_secs;
>
> opendir $DIRHANDLE, "$ARGV[0]" or die "Can't open directory handle: $!";
>
> while ($file = readdir $DIRHANDLE) {


perldoc -f readdir
readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir". If used in list context, returns all the rest of the
entries in the directory. If there are no more entries, returns
an undefined value in scalar context or a null list in list
context.

If you're planning to filetest the return values out of a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
"readdir", you'd better prepend the directory in question.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
Otherwise, because we didn't "chdir" there, it would have been
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^
testing the wrong file.
^^^^^^^^^^^^^^^^^^^^^^

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

> $write_secs = (stat($file))[9];

^^^^^^^^^^

> printf "file %s updated at %s\n", $file,
> scalar localtime($write_secs);
> }
>
> closedir $DIRHANDLE;



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com