Adriano Allora wrote:
hi to all,

does exists a way to rename automatically files?

I mean: I've got directories with this content:

EN0.tmp
EN1.tmp
EN2.tmp
EN3.tmp
EN4.tmp
...
IT0.tmp
IT1.tmp
IT2.tmp
IT3.tmp
IT4.tmp
...

Now, some couples of files were deleted (you can imagine the EN1.tmp/IT1.tmp) but I need no holes in my list.

Is there something less rude (and more general) than

$c = 0;
while(<>)
    {
    if($ARGV=~/^([A-Z]+)(\d+)\.tmp/)
        {
        $nuname = $1.$c.'.tmp';
        `mv $ARGV $nuname';
        $c++;
        }
    }
[snip sig}

Hi Adriano

This isn't the answer you wanted. Depending on your point of view you may consider it even ruder than your code, but the 'automatic' way that you wanted doesn't exist :-/

You appear to have an input file with a list of files that need to be renamed. I've just grabbed a listing of the current directory, and fixed the problems that:

- You may well want the files numbered in the same /order/ as they originally were, which won't be the order they appear in a directory listing.

- It looks like you want a separate numbering sequence for each filename prefix.

This does all those things, and is deliciously rude:

opendir my $dh, '.' or die $!;

my %pfx;
foreach (
    sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] }
    grep /^[A-Z]+\d+\.tmp$/, readdir $dh) {
  /^([A-Z]+)/;
  (my $new = $_) =~ s/\d+/$pfx{$1}++ || 0/e;
  rename $_, $new;
}

Hope this helps or amuses.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to