Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I read files from a folder and place them into a list?

by wstarrs (Acolyte)
on May 10, 2001 at 00:31 UTC ( [id://79239]=perlquestion: print w/replies, xml ) Need Help??

wstarrs has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

I am trying to read files from a specific folder and place them into a list in my Perl code, I then need to take this list and sort it, what is the easiest way to do this? Thanks, Bill

Originally posted as a Categorized Question.

  • Comment on How do I read files from a folder and place them into a list?

Replies are listed 'Best First'.
Re: How do I read files from a folder and place them into a list?
by chromatic (Archbishop) on May 10, 2001 at 00:34 UTC
    Open the directory with opendir, then call sort on your readdir. You might want to use file tests -X to see if you have only files.

    Something like the following may work:

    opendir DIR, $dir or die "Can't open $dir: $!"; my @files = sort grep { -f "$dir/$_" } readdir DIR; # work with @files
Re: How do I read files from a folder and place them into a list?
by Ovid (Cardinal) on May 10, 2001 at 00:37 UTC
    If you want the files in the current directory, you could do something like this:
    my $somedir = '.'; opendir DH, $somedir or die "Cannot open $somedir: $!"; my @files = grep { ! -d } readdir DH; closedir DH;
Re: How do I read files from a folder and place them into a list?
by ctilmes (Vicar) on Apr 22, 2003 at 15:38 UTC
    File::Slurp offers a shortcut for the opendir/readdir/closedir idiom:
    use File::Slurp; my $dir = '.'; # or whatever my @files = sort grep { -f "$dir/$_" } read_dir($dir);
    Note that File::Slurp::read_dir automatically skips '.' and '..'.
Re: How do I read files from a folder and place them into a list?
by Anonymous Monk on May 28, 2004 at 17:15 UTC
    opendir DIR, $dir or die "error: cannot open directory \"$dir\" $!"; @files = sort grep (!/^\.$|^\.\.$/, readdir(DIR)); closedir (DIR);
      Simplified:
      @files = sort grep (!/^\.\.?$/, readdir(DIR));
      cLive ;-)
Re: How do I read files from a folder and place them into a list?
by tachyon (Chancellor) on Apr 23, 2003 at 11:47 UTC
    $dir = 'c:'; @files = grep{-f $_}glob("$dir/*");
Re: How do I read files from a folder and place them into a list?
by Anonymous Monk on Jun 04, 2001 at 09:41 UTC
    Or how about using this? Seems a bit easier and shorter
    my @files = <*>;
    or you can do this if you want a list of files only (not directories)
    my @files=(); while (<*>) { push (@files,$_) if (-f "$_"); }
Re: How do I read files from a folder and place them into a list?
by Anonymous Monk on Apr 22, 2003 at 15:18 UTC
    my @files = `ls`;

    Originally posted as a Categorized Answer.

Re: How do I read files from a folder and place them into a list?
by Anonymous Monk on Oct 25, 2004 at 06:43 UTC
    HOW CAN I USE WITH THIS FILES? I WANT TO TAKE DATA FROM ANY FILE..

    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://79239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-07-16 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.