Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I read a directory?

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 you read a directory?
by damian1301 (Curate) on Mar 25, 2001 at 06:50 UTC
Re: How do you read a directory
by tinman (Curate) on Mar 25, 2001 at 07: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 06:03 UTC
    The tin man answered my question last time I read here.... what happened to his answer?

    Originally posted as a Categorized Answer.

Re: How do I read a directory?
by cLive ;-) (Prior) on Mar 30, 2001 at 05: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 19: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 I read a directory?
by hippo (Archbishop) on Sep 17, 2018 at 07: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$/);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://66983]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.