現在のディレクトリの下で言うとき、あなたは現在のディレクトリ、または現在のディレクトリとその子孫の中または下のどこかを意味しますか?
File::Find
後者を行う簡単な方法であり、コアモジュールであるため、インストールする必要はありません。そのようです:
use strict;
use warnings;
use autodie;
use File::Find;
find(\&rename, '.');
sub rename {
return unless -f;
my $newname = $_;
return unless $newname =~ s/-\(ab-[0-9]+\)(\.txt)$/$1/i;
print "rename $_, $newname\n";
}
アップデート
このプログラムは、現在のディレクトリ内でのみ、指定されたファイル名パターンですべてのファイルの名前を変更します。
最初のopen
ループは、名前を変更するためのサンプルファイルを作成するためだけにあることに注意してください。
use strict;
use warnings;
use autodie;
open my $fh, '>', $_ for qw(
foo-bar-(ab-4529111094).txt
foo-bar-foo-bar-(ab-189534).txt
foo-bar-foo-bar-bar-(ab-24937932201).txt
);
for (glob '*.txt') {
next unless -f;
my $newname = $_;
next unless $newname =~ s/-\(ab-[0-9]+\)(\.txt)$/$1/i;
print "rename $_, $newname\n";
rename $_, $newname;
}
出力
rename foo-bar-(ab-4529111094).txt, foo-bar.txt
rename foo-bar-foo-bar-(ab-189534).txt, foo-bar-foo-bar.txt
rename foo-bar-foo-bar-bar-(ab-24937932201).txt, foo-bar-foo-bar-bar.txt