Reputation: 21
how can i list all files in parent and sub directories for multilpe dirs?
$dir="/home/httpd/cgi-bin/r/met";
opendir(DIR,"/home/httpd/cgi-bin/r/met")||die"error";
while($line=readdir DIR)
{
print"$line\n";
opendir DIR1,"$dir/$line"||die"error";
while($line1=readdir DIR1)
{
print"$line1\n";
}
}
closedir DIR;
closedir DIR1;
Upvotes: 0
Views: 113
Reputation: 1633
File::Find effectively walks through list of directories and executes a subroutine defined by you for each file or directory found recursively below the starting directory. Before calling your subroutine find
(a function exported by File::Find
module) by default changes to the directory being scanned and sets the following (global) variables:
$File::Find::dir
-- visited directory path relative to the starting directory$File::Find::name
-- full path of the file being visited relative to the starting directory$_
-- basename of the file being visited (used in my example)One way to solve your problem would be:
#!/usr/bin/perl
# Usage: ffind [dir1 ...]
use strict; use warnings;
use 5.010; # to be able to use say
use File::Find;
# use current working directory if no command line argument
@ARGV = qw(.) unless @ARGV;
find( sub { say if -f }, @ARGV );
Upvotes: 0
Reputation: 53478
Don't do it this way, use File::Find
instead.
use strict;
use warnings;
use File::Find;
my $search = "/home/httpd/cgi-bin/r/met";
sub print_file_names {
print $_,"\n";
}
find ( \&print_file_names, $search );
Upvotes: 4