Windows Perl:dirhandle的错误符号
这是我的代码:Windows Perl:dirhandle的错误符号,windows,perl,rename,dir,Windows,Perl,Rename,Dir,这是我的代码: opendir(DIR, $directoryPath) or die "Cant open $directoryPath$!"; my @files = readdir(DIR); #Array of file names closedir (DIR) or die "Cant close $directoryPath$!"; 我正在使用@files在目录中创建一个文件名数组,以便稍后在程序中重命名 问题是: 我在closedir行得到错误“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在目录中创建一个文件名数组,以便稍后在程序中重命名
问题是:
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$!";
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
使用5.16.3;
严格使用;
打印“输入目录:”;
我的$directoryPath=;
chomp($directoryPath);
chdir($directoryPath)或die“不能chdir到$directoryPath$!”;
opendir(DIR,$directoryPath)或die“无法打开$directoryPath$!”;
my@files=readdir(DIR)#文件名数组
closedir(DIR)或die“无法关闭$directoryPath$!”;
我的$fileName=“文件”;
对于我的$i(0..@文件)
{
重命名($files[$i],$fileName($i+1))或禁用“无法重命名文件$files[$i]$!”;
}
chdir#返回主目录
我可以正确输入路径,但错误消息(准确复制)如下:
无法重命名文件。在C:\path\to\file\RenameFiles.pl第19行第1行,权限被拒绝。
错误
Can't rename file .Permission denied at C:\path\to\file\RenameFiles.pl line 19, <> line 1.
可以。这将跳过以句点开头的任何文件
。或者,您可以跳过目录:
next if -d $files[$i]; # skip directories (includes . and ..)
正如TLP已经指出的,返回与当前目录和父目录相对应的
和。
为了避免重命名目录,您需要过滤掉这些内容
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;
无法使用给定的代码再现错误。另外,您的代码中仍然有
i
,它应该是$i
。您是否已将chdir
-ed添加到正确的目录中?请举例说明从重命名中得到的错误消息。您添加了代码以避免重命名
和。
对吗?否则,这就是您的权限问题。@TLP添加了代码和确切的错误message@TLP还修复了错误的符号错误,有一些稍微不正确的语法。谢谢,下一个if行在重命名行之前的for循环中会去哪里?是的。您只能在循环中使用next
(和last
),在rename
@ikegami对您进行重命名之后,这样做没有任何好处。我要把那部分去掉。
next if -d $files[$i]; # skip directories (includes . and ..)
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;
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);
}