×
Home About Us Products Services News Free Scripts Contact
news php scripts and software

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


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

# 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: Perl

 
<< Go back