Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How do you list all files under a directory?

by Marshall (Canon)
on Sep 26, 2010 at 11:56 UTC ( [id://862074]=note: print w/replies, xml ) Need Help??


in reply to How do you list all files under a directory?

This glob stuff can get hairy as there are multiple versions of it. I've had some big troubles in the past with this in the Windows environment. Adding a grep to process the list from readdir is portable and is just a few more characters to your standard "formula".

Technically a directory is a file, but -f means the subset of files that aren't directories (which excludes '.' and '..'). Be sure to use a full path name in the grep otherwise you will be testing against files in the directory that the program is executing in, which is probably not what you want! When I do want that, I set $dir = "." and use the same formula.

my $dir="/path/to/dir"; opendir(DIR, $dir) or die "can't open $dir: $!"; my @files = grep {-f "$dir/$_"} readdir DIR; closedir DIR;
Updated: added single quotes to make '.' and '..' more clear in some fonts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found