perl - 无法在 Perl 中打开子目录中的文件

标签 perl file subdirectory

我真的很难过。我是 Perl 的新手,我完全无法尝试遍历所有子目录,执行操作。它在根目录(我的脚本所在的位置)中工作正常,但是一旦到达子目录,它就会爆炸。我试过在 "或 ' 或 q{} 中围绕文件名无济于事。我在 StackOverflow 上搜索了这个问题,人们说它应该可以正常工作,打开处理子目录并将接受正斜杠。 一旦进入目录并尝试打开“./Horse/Models.meta”,它就会在第一次打开(读取文件)时失败。这是在它处理了与我的脚本相同级别的 20 个文件之后。有什么想法吗?

use 5.010;
use strict;
use warnings;
use File::Find;

find(sub {
if (-f and /\.meta$/) {
    print "--> " . $File::Find::name . "\n";
    open my $in,  '<',  $File::Find::name    or die "Can't read old file: $!";
    open my $out, '>', "$File::Find::name.new" or die "Can't write new file: $!";

    while( <$in> )
    {
            if(index($_,"  assetBundleName:")!=-1) {
                    print $out "  assetBundleName: mobs\n";
            } else {
                    print $out $_;
            }
    }
    close $out;
    print("mv -f \"" . $File::Find::name . ".new\" \"" . $File::Find::name . "\"\n");

    system("mv -f \"" . $File::Find::name . ".new\" \"" . $File::Find::name . "\"");

  }
}, '.');

最佳答案

就用

rename "$File::Find::name.new", $File::Find::name or die $!;

参见 rename .

如果你想使用相对路径(即$File::Find::name)而不只是文件名(即$_),你需要指定

find({ wanted   => sub { ... },
       no_chdir => 1,
}, '.');

关于perl - 无法在 Perl 中打开子目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46083908/

相关文章:

java - 将文件中的值与数组进行比较

c++ - C++比较两个文件

perl - 如何获取 Perl/Tk 文本小部件中的文本?

php - 如何使用ssh删除远程文件?

perl - Perl 如何在不显示终端的情况下提示输入密码?

linux - 计算linux中tgz文件中子目录中的文件数

.htaccess - 使用 htaccess 删除子目录中的 codeigniter index.php

html - 从子文件夹中选择根文件夹的图像

linux - 将/etc/yum.repos.d/* 复制到所有计算机上的同一目录,而无需重复输入 root 密码

Perl,计算字符并在单词末尾中断