Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How do I read all the file names of a directory into an array?

by mirod (Canon)
on Dec 10, 2000 at 14:31 UTC ( [id://45937]=note: print w/replies, xml ) Need Help??


in reply to How do I read all the file names of a directory into an array?

Use the directory functions: opendir, closedir and readdir.

opendir DIR, $dir or die "cannot open dir $dir: $!"; my @file= readdir DIR; closedir DIR;

This will neatly put the list of file names in the @file array (with no \n at the end of each file name).

There is a couple of other things you can improve in your code BTW:

  • use Perl style loops over arrays:
    foreach my $file (@file) { print "$file\n"; }
    this is much simpler than using C-style for( $i=0;...) loops, you don't have to use $i and $total any more (BTW $#filenames would give you the last index in the @filenames array, you don't have to use $total)
  • do not use chop but chomp instead, it's safer as it will not remove the last character of a string if it is not a newline.

Finally I have no idea why $filename$i =~ s/\*//g; is there? Is there a chance you that you might find a '*' in filenames?

  • Comment on Re: How do I read all the file names of a directory into an array?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How do I read all the file names of a directory into an array?
by Ri-Del (Friar) on Dec 10, 2000 at 19:47 UTC
    Wow! Everyone thank you ever so much for your help, I've been learning so much! Yes, there is a chance I will find a '*' in the filenames. For some reason when I am connected to my department's server (running RedHat 7.0) and I 'ls' a directory some of the files (not all of them) have a '*' after their name. I'm not quite sure why, but I knew I needed to get rid of them to craft the link correctly for the webpage.
      That's a perfect example of why you should be reading the directory listing from within Perl (i.e. the opendir/readdir solution), rather than using a non-portable external command.

      On the department's server, `ls` is aliased to `ls -F`. The -F option causes ls to add * to executables, / to directories, and @ to symbolic links. Useful when looking at a directory listing from the command prompt; not useful when getting a list of filenames within Perl.

      I believe that the reason there are *'s after some files is because ls is aliased to ls -F, which will add a * to the end of files that are executeable. It will also add some other characters onto files that are directories, symlinks, etc.... You won't run into that using readdir though. Hope this helps!

      - Brad

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found