【问题标题】:rename the txt file extension using perl使用 perl 重命名 txt 文件扩展名
【发布时间】:2013-05-28 11:57:35
【问题描述】:

我正在执行以下步骤:

读取目录中的所有文本文件并将其存储在名为@files 的数组中 在每个文本文件上运行一个 foreach 循环。使用拆分操作提取文件名(剥离 .txt)并创建该特定文件名的文件夹。将该文件重命名为 Test.txt(以便作为另一个 perl 可执行文件的输入) 通过添加行为每个文件执行 test.pl require "test.pl"; 它仅适用于一个文件,但不再适用。这是我的代码:

opendir DIR, ".";
my @files = grep {/\.txt/} readdir DIR;

foreach my $files (@files) {

    @fn = split '\.', $files;
    mkdir "$fn[0]" 
        or die "Unable to create $fn[0] directory <$!>\n";

    rename "$files", "Test.txt";
    require "test3.pl";

    rename "Test.txt", "$files";
    system "move $files $fn[0]";
}

【问题讨论】:

标签: perl


【解决方案1】:

你不是require文件要加载一次,而是done每次都加载。

所以,替换

需要“test3.pl”;

做“test3.pl”;

【讨论】:

    【解决方案2】:

    你能在那个目录中查找文件吗..

    替换,

    opendir DIR, ".";
    my @files = grep {/\.txt/} readdir DIR;
    

    与,

    my @files = <*.txt>;
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2017-04-23
      • 2014-05-29
      • 2016-11-06
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多