Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 ( #79239=categorized question: print w/replies, xml ) Need Help??
Contributed by wstarrs on May 10, 2001 at 00:31 UTC
Q&A  > arrays


Description:

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

Answer: How do I read files from a folder and place them into a list?
contributed by chromatic

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
Answer: How do I read files from a folder and place them into a list?
contributed by Ovid

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;
Answer: How do I read files from a folder and place them into a list?
contributed by ctilmes

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 '..'.
Answer: How do I read files from a folder and place them into a list?
contributed by Anonymous Monk

opendir DIR, $dir or die "error: cannot open directory \"$dir\" $!"; @files = sort grep (!/^\.$|^\.\.$/, readdir(DIR)); closedir (DIR);
Answer: How do I read files from a folder and place them into a list?
contributed by tachyon

$dir = 'c:'; @files = grep{-f $_}glob("$dir/*");
Answer: How do I read files from a folder and place them into a list?
contributed by Anonymous Monk

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 "$_"); }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2016-10-01 21:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?






      Results (8 votes). Check out past polls.