0

I have a script that reads the contents of a directory. But the script can't open the directory because permission is denied. I am working on Windows, and I've tried to run the script as administrator, but that didn't help.

Here's the code:

sub dir_put {
  my $dir_name = shift;

  open DIR, $dir_name or die "Error reading directory: $!";
  my @array;
  my @return;

  while ($_ = readdir(DIR)){
    next if $_ eq "." or $_ eq "..";
    if (-d $_) {
      @return = dir_put($_);
      unshift(@array, @return);
      next;
    }
    unshift (@array, "$dir_name\\$_");
  }

  @array;
}

How should I fix it?

Borodin
  • 126,100
  • 9
  • 70
  • 144
brotheroftux
  • 87
  • 1
  • 2
  • 9

2 Answers2

4

I think you want opendir, not open.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
1

You can't open directory with open, it isn't file. For opening directories there is opendir function in perl.

Try:

opendir my $dir, $dir_name or die "Error reading directory: $!";
my @array;
my @return;
while ( readdir $dir ) {
...

Also you better use modules from cpan like File::Find or File::Find::Rule

perl -MFile::Find::Rule -E "say $_ for File::Find::Rule->in('F:\\Films');"
Suic
  • 2,441
  • 1
  • 17
  • 30