Fri, 17 May 2024


How do I recurse through the files in a directory tree?

By: Peter, NetArt Media
Sun, 30 August 2020

# here's the File::Find way
use File::Find;
find(sub { print "$File::Find::name\n" }, @ARGV);

# here's the do-it-yourself way
sub do_file
{
my ($path, $name) = @_;
print "$path/$name\n";
return unless -d("$path/$name");
local *DIRH;
opendir DIRH, "$path/$name"
or warn("couldn't open $path/$name: $!"), return;
my @files = grep { $_ ne '.' and $_ ne '..' } readdir DIRH;
closedir DIRH;
for (@files)
{
do_file("$path/$name", $_);
}
}


for (@ARGV)
{
do_file(".", $_);
}


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox