Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Any alternatives to Opendir / Readdir?

464 views
Skip to first unread message

12

unread,
Aug 22, 2000, 3:00:00 AM8/22/00
to
Please does anyone know of good alternatives to opendir / readdir, as I find
them rather limited.

I would like a routine that returned one array containing all information on
each file and sub-directory, including type, date/time etc.

I'm running Active Perl on Win NT.

Any suggestions?

Andy Turpin

Malcolm Dew-Jones

unread,
Aug 22, 2000, 3:00:00 AM8/22/00
to
Martien Verbruggen (mg...@tradingpost.com.au) wrote:
: On Tue, 22 Aug 2000 11:38:04 +0100,
: 1...@123.com <1...@123.com> wrote:
: > Please does anyone know of good alternatives to opendir / readdir, as I find
: > them rather limited.

: Oh.. Maybe one more thing: A stat is a fairly expensive operation. It is

On windows systems the stat should be no more expensive than the name
lookup as the information is stored in the same place.

If you were working at a lower level then you would get all the file
details during the same disk access.


nob...@mail.com

unread,
Aug 22, 2000, 3:00:00 AM8/22/00
to
"12" <1...@123.com> writes:

> Please does anyone know of good alternatives to opendir / readdir, as I find
> them rather limited.

File::Find



> I would like a routine that returned one array containing all information on
> each file and sub-directory, including type, date/time etc.

Unless you want to recursively decend directories File::Find is
overkill for something so simple:

sub get_dir {
my $dir = shift;
local *DIR;
opendir(DIR,$dir) or die "Cannot read directory $dir: $!";
map { [ $_, stat "$dir/$_" ] } readdir DIR;
}

Note: I should really be checking that stat() succedes.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\

Martien Verbruggen

unread,
Aug 22, 2000, 10:46:32 AM8/22/00
to
On Tue, 22 Aug 2000 11:38:04 +0100,
1...@123.com <1...@123.com> wrote:
> Please does anyone know of good alternatives to opendir / readdir, as I find
> them rather limited.
>
> I would like a routine that returned one array containing all information on
> each file and sub-directory, including type, date/time etc.

You'll have to write one yourself. opendir, readdir and closedir,
together with stat will do what you want. I wouldn't want this stuff to
come back in one array though.. You better have a bit more thought about
what your data structure should be.

Oh.. Maybe one more thing: A stat is a fairly expensive operation. It is

often much better to only do a stat on a file or directory for which you
think you are going to need the information. I guess that that is one of
the reasons why there isn't such a module as you suggest, and why i
wouldn't even want to think of writing one.

opendir(DIR, $dir) or die "opendir($dir): $!";
while(my $file = readdir<DIR>)
{
# weed out the bad ones
my @stat = stat "$dir/$file";
# maybe store the info somewhere?
}
closedir(DIR);

Can you tell me what is so horrible about having to write that code? I
count about 4 lines of significant code there....

Martien
--
Martien Verbruggen |
Interactive Media Division | life ain't fair, but the root
Commercial Dynamics Pty. Ltd. | password helps. -- BOFH
NSW, Australia |

Martien Verbruggen

unread,
Aug 22, 2000, 8:05:33 PM8/22/00
to
On 22 Aug 2000 10:08:03 -0800,
Malcolm Dew-Jones <yf...@vtn1.victoria.tc.ca> wrote:
> Martien Verbruggen (mg...@tradingpost.com.au) wrote:
> : On Tue, 22 Aug 2000 11:38:04 +0100,
> : 1...@123.com <1...@123.com> wrote:

> : > Please does anyone know of good alternatives to opendir /
> : > readdir, as I find them rather limited.
>

> : Oh.. Maybe one more thing: A stat is a fairly expensive operation.


>
> On windows systems the stat should be no more expensive than the
> name lookup as the information is stored in the same place.

Well.. You'd still need a separate stat for the file themselves, but
assuming you're right:

opendir(DIR, $dir) or die $!;
%files =
map { ("$dir/$_" => [stat "$dir/$_"]) }
grep { !/^..?$/ }
readdir(DIR);
closedir(DIR);

while (my ($k, $v) = each %files)
{
printf "$k => %s\n", $v->[1] || 'UNDEF';
}

or if you are interested in the order:

opendir(DIR, $dir) or die $!;
my @files =
map { ["$dir/$_" => [stat "$dir/$_"]] }
grep { !/^..?$/ }
readdir(DIR);
closedir(DIR);

foreach (@files)
{
printf "$_->[0] => %s\n", $_->[1]->[1] || 'UNDEF';
}

Of course anyone is free to wrap this in subs or classes/objects if
they feel like it, but I still maintain that the module would be
expensive to use, and anyone implementing that should make note of
that in the documentation.

Oh, and you should probably use File::Spec to make it more portable.

> If you were working at a lower level then you would get all the file
> details during the same disk access.

but you're not working at a lower level :)

Martien
--
Martien Verbruggen |
Interactive Media Division | 42.6% of statistics is made up on the
Commercial Dynamics Pty. Ltd. | spot.
NSW, Australia |

___cliff rayman___

unread,
Sep 20, 2000, 1:51:20 AM9/20/00
to
try:
perldoc File::Find

12 wrote:
>
> Please does anyone know of good alternatives to opendir / readdir, as I find
> them rather limited.
>

> I would like a routine that returned one array containing all information on
> each file and sub-directory, including type, date/time etc.
>

> I'm running Active Perl on Win NT.
>
> Any suggestions?
>
> Andy Turpin

--
_____cliff_rayman_____cliff_@_rayman_._com_____

0 new messages