http://www.perlmonks.org?node_id=508790


in reply to Re: How to create the varibles with the file names
in thread How to create the varibles with the file names

I'd use File::Find

#!/usr/bin/perl -w use strict; use File::Find; my $dir = "C:\\data\\"; find(\&Do_Something, $dir); sub Do_Something{ if ( -f && /\.txt$/ ) { print "Doing Something $_ \n"; } }

Replies are listed 'Best First'.
Re^3: How to create the varibles with the file names
by duff (Parson) on Nov 15, 2005 at 21:10 UTC

    Except that File::Find recurses into subdirectories and the OP didn't indicate that recursion was needed.

    Also, there's more conceptual baggage with File::Find. If I'm going to gather the files into an array, how do I do that? How do I not recurse if that's appropriate? Et cetera. It's alot more for someone to have to figure out over a simple glob or readdir

Re^3: How to create the varibles with the file names
by mrpeabody (Friar) on Nov 16, 2005 at 16:26 UTC
    Well, if you're going to use File::Find, you might as well use the saner syntax of File::Find::Rule.
    #!/usr/bin/perl use warnings; use strict; use File::Find::Rule; my $dir = "C:/Windows"; my @files = File::Find::Rule ->file() ->name('*.txt') ->mindepth(1) ->maxdepth(1) ->in($dir); print "$_\n" for @files;
    For a job this simple, though, I would probably use glob().