bkiahg has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to pull all of the directories out of an array. I'm using perl 5.8 on a win 2000 box.
opendir(IMD, $dirtoget) || die("Cannot open directory"); @thefiles = readdir(IMD); closedir(IMD); foreach $f (@thefiles) { if (-d $f) { print "$f<br>\n"; } }
All it's printing is the . and .. Any suggestions?

Replies are listed 'Best First'.
Re: Problem pulling directories out of an array.
by Fletch (Bishop) on Feb 02, 2004 at 18:08 UTC

    Prefix $dirtoget on the filenames. perldoc -f readdir

    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.
    foreach my $f ( map { "$dirtoget/$_" } @thefiles ) { ... }
Re: Problem pulling directories out of an array.
by tachyon (Chancellor) on Feb 02, 2004 at 18:09 UTC

    The issue is that readdir() returns just the file/dir name in the target dir so you need to add the dir path to your -d test

    if ( -d "$dirtoget/$f" )

    You could of course just do this:

    # dirs with full path print "$_<br>\n" for grep{ -d $_ } glob($dirtoget); # just the relative path print "$_<br>\n" for map{s/\Q$dirtoget\E//;$_}grep{-d $_}glob($dirtoge +t);

    map grep and glob are useful perl power tools....

    cheers

    tachyon

Re: Problem pulling directories out of an array.
by welchavw (Pilgrim) on Feb 02, 2004 at 18:48 UTC

    Directory handles are just fine, but I prefer the brevity provided by glob (though the Camel notes that readdir is faster, my typical scripts are such that "the less noise, the better").

    use File::Glob qw(:globally); print join "\n", map {-d $_ ? $_ : ()} glob ('c:/foo/{*,.*}');

    ,welchavw

Re: Problem pulling directories out of an array.
by duff (Parson) on Feb 02, 2004 at 18:12 UTC

    What Fletch siad about readdir. This is how I typically handle it however:

    chdir $dirtoget; # Notice! opendir(IMD, '.') || die("Cannot open directory"); @thefiles = readdir(IMD); closedir(IMD); foreach $f (@thefiles) { if (-d $f) { print "$f<br>\n"; } }

    Sometimes chdir isn't the best answer, but it often is.

Re: Problem pulling directories out of an array.
by Limbic~Region (Chancellor) on Feb 02, 2004 at 18:11 UTC
    bkiahg,
    I am guessing that it is because those are the only directories you have in $dirtoget. If you wanted to see files too you would not test with -d. See the following:
    #!/usr/bin/perl -w use strict; my $dirtoget = './CSOC'; opendir( IMD , $dirtoget ) or die "Unable to open $dirtoget : $!"; for my $file ( readdir IMD ) { my $fqp = $dirtoget . '/' . $file; print -d $fqp ? "$fqp is a directory\n" : "$fqp is not a directory +\n"; }
    Cheers - L~R

    Update: Added $dirtoget to directory test as noted by other monks

      Actually I'm spidering a directory and updating a web page and I needed a better way to tell between dir and files than the abscence of .abc The above code is what I needed. Thanks!