[Perl] glob/readdir

Evgeny Goldin geniek at netvision.net.il
Mon Aug 27 07:06:23 PDT 2001


n> i need to be able to tell wether a listed file is a directory or normal
n> file...

Here's my way :

E:\Temp>cat 1.pl

my $dir = shift;

opendir ( DIR, $dir ) or die "111111";
my @dirs = grep { -d }                                     # select directories only
              map { "$dir/$_" }                            # IMPORTANT - prepend directory name
                 grep { ( $_ ne '.' ) and ( $_ ne '..' ) } # throw out dots entries
                    readdir DIR;
closedir ( DIR ) or warn "222222";

local $, = "\n";
print 'Dirs : ', @dirs, "\n";



E:\Temp>perl -w 1.pl E:/Data
Dirs :
E:/Data/Books
E:/Data/C
E:/Data/CGI
E:/Data/CVS
E:/Data/Java
E:/Data/Misc
E:/Data/MySQL
E:/Data/Ora
E:/Data/Perl
E:/Data/RCS
E:/Data/TCL
E:/Data/Treepad


E:\Temp>




More information about the Perl mailing list