Perl:dirhandle的坏符号 [英] Perl: Bad Symbol for dirhandle

查看:201
本文介绍了Perl:dirhandle的坏符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

我使用@files创建目录中的文件名数组,以便以后重命名该程序。

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

问题是:


  1. 我收到错误在闭路线上的dirhandle的坏符号。

  2. 如果我没有关闭以避免这种情况,我没有权限更改文件名(我正在使用Windows)。

  3. 我尝试了一种替代方式来重命名文件(以下),通过以不同的方式重命名文件,并在dirhandles中尝试不同的解决方案,但这只是重复权限错误。

  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$!";


从一大堆研究中我认为权限错误是一个Windows安全功能,因此您无法在其打开时编辑文件,但我无法找到一个足够简单的解决方案。

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.

对于第1点或第3点的回答是可取的,但对第2点的回答也将是有用的。

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

下面的点1.和2.中使用的完整代码

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.


推荐答案

错误

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

表示您尝试重命名文件 code>,这是一个特殊文件,它是当前目录的快捷方式。你应该添加例外到你的代码不重命名这个文件,一个叫 .. 。如下所示:

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 ..)

这篇关于Perl:dirhandle的坏符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆