Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I read a directory?

by Anonymous Monk
on Mar 25, 2001 at 11:29 UTC ( #66983=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (directories)

How do I read a directory?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I read a directory?
by hippo (Bishop) on Sep 17, 2018 at 11:13 UTC

    With Path::Tiny and using the children method:

    use Path::Tiny 0.028; my $dir = path ('/path/to/dir'); # Retrieve all entries except '.' and '..' my @all = $dir->children (); # Alternatively, retrieve a subset - here just the perl scripts my @perl = $dir->children (qr/\.pl$/);
Re: How do you read a directory?
by damian1301 (Curate) on Mar 25, 2001 at 11:50 UTC
Re: How do I read a directory?
by cLive ;-) (Prior) on Mar 30, 2001 at 10:37 UTC

    Use the readdir function. To read only certain files, you can use it with grep. For example:

    my $dir = '/path/to/dir'; # read all entries: opendir DIR, $dir or die "read dir $dir - $!"; my @dir = readdir DIR; closedir DIR; # read only cgi scripts: opendir DIR, $dir or die "read dir $dir - $!"; my @cgi_scripts = grep /\.cgi$/, readdir DIR; closedir DIR; # read all but (UNIX style) hidden entries: opendir DIR, $dir or die "read dir $dir - $!"; my @dir = grep !/^\./, readdir DIR; closedir DIR;
Re: How do you read a directory
by Sherlock (Deacon) on Apr 19, 2001 at 23:20 UTC
    This is very good, but there is one error:

    open DIR, $dir || die $!;
    should be:

    opendir DIR, $dir || die $!;


    - Sherlock
Re: How do you read a directory
by tinman (Curate) on Mar 25, 2001 at 12:17 UTC
    this node could help too..

    yes, shameless self advertisement, I know :o)

    Originally posted as a Categorized Answer.

Re: How do you read a directory
by Anonymous Monk on Mar 27, 2001 at 11:03 UTC
    The tin man answered my question last time I read here.... what happened to his answer?

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://66983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2023-01-28 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?