如何在目录下的所有文件名中添加一个前缀? 我正在尝试将字符串(Reference_)的前缀前缀到所有目录中所有 *.bmp文件的名称以及子目录中的所有 *.bmp文件的名称。我们第一次运行丝绸脚本时,它将创建目录为...

问题描述 投票:0回答:2
扩展中。

当我第二次运行自动丝网时,它将在所有子目录中再次创建 *.bmp文件。在第二次运行脚本之前,我想将所有 *.bmp列入字符串

reference_
。 例如,例如
first_screen.bmp
我的目录结构如下:

reference_first_screen.bmp

having

C:\Image_Repository\BG_Images\second ... C:\Image_Repository\BG_Images\sixth

first_screen.bmp
文件等...
如何将所有映像文件名与

first_screen.bmp
字符串前缀?
当我第二次运行脚本时,Silk中的Perl脚本将同时从子目录中获取图像,并通过像素来比较它们两个像素。 我正在尝试下面的代码。我该如何完成此任务?

reference_
我尝试使用'+
打开通话
    

您不要打开写作目录。只需使用字符串的模式部分使用

opendir

#!/usr/bin/perl -w &one; &two; sub one { use Cwd; my $dir ="C:\\Image_Repository"; #print "$dir\n"; opendir(DIR,"+<$dir") or "die $!\n"; my @dir = readdir DIR; #$lines=@dir; delete $dir[-1]; print "$lines\n"; foreach my $item (@dir) { print "$item\n"; } closedir DIR; } sub two { use Cwd; my $dir1 ="C:\\Image_Repository\\BG_Images"; #print "$dir1\n"; opendir(D,"+<$dir1") or "die $!\n"; my @dire = readdir D; #$lines=@dire; delete $dire[-1]; #print "$lines\n"; foreach my $item (@dire) { #print "$item\n"; $dir2="C:\\Image_Repository\\BG_Images\\$item"; print $dir2; opendir(D1,"+<$dir2") or die " $!\n"; my @files=readdir D1; #print "@files\n"; foreach $one (@files) { $one="reference_".$one; print "$one\n"; #rename $one,Reference_.$one; } } closedir DIR; }

,无论您不需要。您可以使用file :: find列出所需的文件列表。

#!/usr/bin/perl 使用严格; 使用警告; 使用文件:: basename; 使用文件:: find; 使用文件:: find :: clinures qw(find_regular_files); 使用file :: spec :: functions qw(catfile); 我的($ wath,$ reporter)= find_regular_files; 查找($ want,$ argv [0]); 我的$ prefix ='recursive_'; for erach我的$ file($ reporter->()) { 我的$ basename = basename($ file); if(index($ basename,$ prefix)== 0) { 打印stderr“ $ file已经具有'$ prefix'!跳过。 ” 下一个; } 我的$ new_path = catfile( dirname($ file), “递归_ $ basename” ); 除非(重命名$ file,$ new_path) { 打印stderr“不能重命名$ file:$! ” 下一个; } 打印$文件,” ” }

<' mode but I am getting compilation error for the read and write mode. When I am running this code, it shows the files in BG_images folder with prefixed string but actually it's not updating the files in the sub-directories.

perl file batch-rename
2个回答
3
投票
您可能应该查看

file ::find模块 - 它将使在目录树上上下递归。 您可能应该扫描文件名并修改不以opendir my($dir), $dirname or die "Could not open $dirname: $!";

的开头的文件名,以便它们这样做。 这可能需要将文件名分配到目录名称和文件名中,然后将文件名与
reference_
一起将文件名零件前缀。 这是通过::basename

模块完成的。

在某一点上,您需要确定第三次运行脚本时会发生什么。 已经开始覆盖的文件是否已经被覆盖,或者未修复的文件被覆盖,或者是什么?
文件未重命名的原因是,将重命名操作评论。  切记在脚本的顶部添加
reference_

3
投票
reference_

选项)。 如果您在数组中获取文件列表use strict;

(名称是基本名称,因此您不必使用file :: basename),那么循环可能看起来像:

-w

在windowscoreutils的帮助下

@files

   foreach my $one (@files)
   {
        my $new = "reference_$one";
        print "$one --> $new\n";
        rename $one, $new or die "failed to rename $one to $new ($!)";
   }

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.