views:

179

answers:

6

Hi,

I am running a Perl script and try 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 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 similar for loop as in Perl script?

Thanks.

-joey

+7  A: 

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++;
}
toolic
+8  A: 
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
Greg Bacon
I kinda like your usage of fat comma (=>) here to show the old file becoming new :)
toolic
And to think people say Perl code is unreadable!
Greg Bacon
Of course, what happens when you write `push $element => @list`. Oops, wrong. Be careful with the fat comma.
jrockway
@jrockway, your example is wrong because the 1st arg to push must be an array, not a scalar. This is also wrong: push $element, @list. If you swap the order of args, then fat comma is ok: push @list => $element.
toolic
+1  A: 

Hello,

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";
}
Grimmy
Greg Bacon
Thanks for the advice :) So, you'd rather see something like this?:/(.+)\.ru\.jp/
Grimmy
Yes, or maybe emphasize the rename by putting it out front: `rename ... if /(.+)\.ru\.jp/`. Both ignore the value returned from `rename`, so any failures will be dangerously silent. You could write `/.../ `, but that seems a little cutesy. Another important habit to develop is to always check the return values of system calls, e.g., `rename`, `open`, `unlink`, etc. Even `print` and `close` can fail, so check them when writing important bits.
Greg Bacon
+1  A: 
$count = 1;
for (<*.ru.jp>)
{
        ($filename)=(/^(.*?)\.ru.jp$/);
        rename $_,$filename."t".$count++.".ru.jp";
}
codaddict
+1  A: 
use strict;
my $c=0;
rename("$1.ru.jp", "$1" . $c++ . ".ru.jp") while <*.ru.jp> =~ /(.+).ru.jp/;
Good to use strict; would also use warnings.
Brian Carlton
+1  A: 
my $counter=0;
while(my $file=<*.ru.jp>){
    $counter++;
    my ($front,$back) = split /\./,$file,2;
    $newname="$front$counter".".t."."$back\n";
    rename $file $newname;
}
ghostdog74
Suggestion: my $counter++
Brian Carlton