如何在for循环中重命名文件

发布于 2024-08-15 08:44:39 字数 440 浏览 12 评论 0原文

我正在运行 Perl 脚本并尝试完成重命名文件,如下所示。

我的文件夹中有一个 *.ru.jp 文件列表,其中包含其他不相关的文件。我想用我作为计数器变量获得的数字进行重命名。

在 Bash 中,我会这样做:

for i in $(ls *.ru.jp); do x=${i%%.*}; mv $i  "$x"t"$counter".ru.jp ;done

例如,如果计数器为 1,myfile.ru.jp 将被重命名为 myfilet1.ru.jp。“t”只是表示 t1、t2...等的命名。最重要的是有一个外部循环,随着计数器变量的增加,最终将标记 mafilet2.ru.jp 等等。

我想知道如何编写和表示与 Perl 脚本中类似的 for 循环?

I am running a Perl script and trying to accomplish renaming files as below.

I have a list of *.ru.jp files in a folder with other non-related files. I would like to rename with a number which I have got as a counter variable.

In Bash, I would do it as:

for i in $(ls *.ru.jp); do x=${i%%.*}; mv $i  "$x"t"$counter".ru.jp ;done

E.g., myfile.ru.jp would be renamed as myfilet1.ru.jp if the counter is 1. "t" is just a naming to indicate t1,t2...etc. And there is an outer loop above all which eventually will label mafilet2.ru.jp and so on as the counter variable increases.

I would like to know how could I write and represent a similar for loop as in a Perl script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

川水往事 2024-08-22 08:44:39
perl -e 'for $old (@ARGV) {
           ++$counter;
           if (($new=$old) =~ s/(\.ru\.jp)\z/t$counter$1/) {
             rename $old => $new or warn "$0: rename: $!\n";
           }
         }' *.ru.jp
perl -e 'for $old (@ARGV) {
           ++$counter;
           if (($new=$old) =~ s/(\.ru\.jp)\z/t$counter$1/) {
             rename $old => $new or warn "$0: rename: $!\n";
           }
         }' *.ru.jp
何以心动 2024-08-22 08:44:39

您可以使用 Perl 的文件 glob 和内置 rename 函数,如下所示:

use warnings;
use strict;

my $i = 1;
for (<*.ru.jp>) {
    my $file = $_;
    s/\.ru\.jp$//;
    my $new = $_ . 't'. $i . '.ru.jp';
    rename $file, $new or die "Can not rename $file as $new: $!";
    $i++;
}

You could use Perl's file glob and built-in rename function as follows:

use warnings;
use strict;

my $i = 1;
for (<*.ru.jp>) {
    my $file = $_;
    s/\.ru\.jp$//;
    my $new = $_ . 't'. $i . '.ru.jp';
    rename $file, $new or die "Can not rename $file as $new: $!";
    $i++;
}
她说她爱他 2024-08-22 08:44:39

我尝试了这个,它似乎可以完成工作:

#! /usr/bin/perl

my $count = 0;
for (<*.ru.jp>)
{
        $count++;
        /(.+)\.ru\.jp/;
        rename $_, $1 . "t" . $count . ".ru.jp";
}

I tried this and it seems to do the job:

#! /usr/bin/perl

my $count = 0;
for (<*.ru.jp>)
{
        $count++;
        /(.+)\.ru\.jp/;
        rename $_, $1 . "t" . $count . ".ru.jp";
}
一枫情书 2024-08-22 08:44:39
$count = 1;
for (<*.ru.jp>)
{
        ($filename)=(/^(.*?)\.ru.jp$/);
        rename $_,$filename."t".$count++.".ru.jp";
}
$count = 1;
for (<*.ru.jp>)
{
        ($filename)=(/^(.*?)\.ru.jp$/);
        rename $_,$filename."t".$count++.".ru.jp";
}
少女情怀诗 2024-08-22 08:44:39
use strict;
my $c=0;
rename("$1.ru.jp", "$1" . $c++ . ".ru.jp") while <*.ru.jp> =~ /(.+).ru.jp/;
use strict;
my $c=0;
rename("$1.ru.jp", "$1" . $c++ . ".ru.jp") while <*.ru.jp> =~ /(.+).ru.jp/;
椵侞 2024-08-22 08:44:39
my $counter=0;
while(my $file=<*.ru.jp>){
    $counter++;
    my ($front,$back) = split /\./,$file,2;
    $newname="$front$counter".".t."."$back\n";
    rename $file $newname;
}
my $counter=0;
while(my $file=<*.ru.jp>){
    $counter++;
    my ($front,$back) = split /\./,$file,2;
    $newname="$front$counter".".t."."$back\n";
    rename $file $newname;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文