Perl: Bad Symbol for dirhandle

自闭症网瘾萝莉.ら 提交于 2019-12-25 02:37:24

问题


This is my code:

opendir(DIR, $directoryPath) or die "Cant open $directoryPath$!";
my @files = readdir(DIR); #Array of file names
closedir (DIR) or die "Cant close $directoryPath$!";

I'm using @files to create an array of the file names within the directory for renaming later in the program.

The problem is:

  1. I am getting the error "Bad Symbol for dirhandle" at the closedir line.
  2. If I don't closedir to avoid this, I don't have permission to change file names (I'm using Windows).
  3. I tried an alternative way of renaming the files (below) to try a different solution to the problem by renaming the files a different way and within the dirhandles, but this just repeat the permission errors.

    opendir(DIR, $directoryPath) or die "Cant open $directoryPath$!";
    while( (my $filename = readdir(DIR)))
    {
        rename($filename, $nFileName . $i) or die "Cant rename file $filename$!";
        i++;
    }
    closedir (DIR) or die "Cant close $directoryPath$!";
    

From a quick bit of research I think the permission error is a Windows security feature so you can't edit a file while its open, but I haven't been able to find a solution simple enough for me to understand.

An answer to point 1. or point 3. is preferrable, but an answer to point 2. will also be useful.

Full code used in points 1. and 2. below

use 5.16.3;
use strict;

print "Enter Directory: ";
my $directoryPath = <>;
chomp($directoryPath);
chdir("$directoryPath") or die "Cant chdir to $directoryPath$!";

opendir(DIR, $directoryPath) or die "Cant open $directoryPath$!";
my @files = readdir(DIR); #Array of file names
closedir (DIR) or die "Cant close $directoryPath$!";

my $fileName = "File ";

for my $i (0 .. @files)
{
    rename($files[$i], $fileName . ($i+1)) or die "Cant rename file $files[$i]$!";
}

chdir; #return to home directory

I can input the path correctly, but then error message (copied exactly) is:

Can't rename file .Permission denied at C:\path\to\file\RenameFiles.pl line 19, <> line 1.

回答1:


The error

Can't rename file .Permission denied at C:\path\to\file\RenameFiles.pl line 19, <> line 1.

says that you are trying to rename the file ., which is a special file that is a shortcut for "current directory". You should add exceptions to your code to not rename this file, and the one called ... Something like:

next if $files[$i] =~ /^\./;

Would do. This will skip over any file that begins with a period .. Alternatively you can skip directories:

next if -d $files[$i];   # skip directories (includes . and ..)



回答2:


As TLP has already pointed out, readdir returns . and .. which corresponds to the current and parent directory.

You'll need to filter those out in order to avoid renaming directories.

use strict;
use warnings;
use autodie;

print "Enter Directory: ";
chomp( my $dirpath = <> );

opendir my $dh, $dirpath or die "Can't open $dirpath: $!";

my $number = 0;

while ( my $file = readdir($dh) ) {
    next if $file =~ /^\.+$/;
    my $newfile = "$dirpath/File " . ++$number;
    rename "$dirpath/$file", $newfile or die "Cant rename file $file -> $newfile: $!";
}

closedir $dh;

Cross Platform Compatibility using Path::Class

One way to simplify this script and logic is to use Path::Class to handle file and directory operations.

use strict;
use warnings;
use autodie;

use Path::Class;

print "Enter Directory: ";
chomp( my $dirname = <> );

my $dir = dir($dirname);

my $number = 0;

for my $file ( $dir->children ) {
    next if $file->is_dir();
    my $newfile = $dir->file( "File" . ++$number );
    $file->move_to($newfile);
}


来源:https://stackoverflow.com/questions/25833120/perl-bad-symbol-for-dirhandle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!