Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows Perl:dirhandle的错误符号_Windows_Perl_Rename_Dir - Fatal编程技术网

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在目录中创建一个文件名数组,以便稍后在程序中重命名

问题是:

  • 我在closedir行得到错误“dirhandle的坏符号”
  • 如果我不关闭目录来避免这种情况,我就没有更改文件名的权限(我使用的是Windows)
  • 我尝试了另一种重命名文件的方法(如下所示),通过在dirhandles中以不同的方式重命名文件来尝试不同的解决方案,但这只是重复了权限错误

    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安全功能的一部分,因此您无法在文件打开时编辑文件,但我还没有找到一个简单到我能理解的解决方案

    对第一点的回答。或第3点。更可取,但这是对第2点的回答。也将是有用的

    第1点中使用的完整代码。二,。下面

    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);
    }