Re: Create a list of files in a directory (recursively)

[email protected] ("Jeff Peng")
Newsgroups perl.beginners
Message-ID <[email protected]>
On Sat, Jun 7, 2008 at 11:30 PM, moroshko <[email protected]> wrote:
> Hello experts !
>
> What is the most efficient way to get a list of all file names (a full
> path) in a certain directory ?
> This should work recursively and include only files (not directories).

Hi,

Just show a way.
The codes below,

use Filesys::Tree qw/tree/;

my $tree = tree({ 'full' => 1,'max-depth' => 3,'pattern' => qr/\.pl$/ } ,'.');
files($tree);

sub files
{
    my %tree = %{+shift};
    for (keys %tree) {
        if ($tree{$_}->{type} eq 'f'){
            print $_,"\n"
        }elsif ($tree{$_}->{type} eq 'd') {
            files($tree{$_}->{contents});
        }
    }
}

should get what you wanted. It's the same as this unix command do,

$ find . -type f -name "*.pl" -maxdepth 2

HTH.

-- 
Jeff Peng - [email protected]
Professional Squid supports in China
http://www.ChinaSquid.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.