brotheroftux
brotheroftux

Reputation: 87

Permission denied while reading contents of a Windows directory

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?

Upvotes: 0

Views: 1456

Answers (2)

Suic
Suic

Reputation: 2501

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');"

Upvotes: 1

Joe Z
Joe Z

Reputation: 17946

I think you want opendir, not open.

Upvotes: 4

Related Questions