| 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
|