Use glob or opendir and readdir to get a list of filenames, then loop through the list.

For example,

my $dir = '.' my @list = glob($dir . '/*'); # <- amended per merlyn's response
or
my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; @list = readdir($dh); closedir($dh);
will return a list of files in the current working directory. My preference is to use glob for this sort of thing. Usually, the number of files in a directory will be small (no more than a few thousand), so storing the names in an array should not be a problem. You may want to filter @list, so that it contains only the type of file you want, but this is easy using grep. Glob can process wildcards, so @list could be restricted by something like
@list = glob($dir .'/A*');

Alternatively, you could call readdir in a scalar context, and process the files one at a time. The code for this would look something like:
my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; while(readdir($dh)){ # do stuff to $_ } closedir($dh);

As I said, since the number of files in a folder tends to be fairly small, I prefer to use glob, possibly in conjunction with grep to get a list of file names, and then loop through that list.


Corrected call to glob; see merlyn's response.

emc

Insisting on perfect safety is for people who don't have the balls to live in the real world.

—Mary Shafer, NASA Dryden Flight Research Center

In reply to Re: getting files names by swampyankee
in thread getting files names by uvnew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.