Hi,
The line:
$new =~ s/(.*)(.jpg)/$count$2/;
is bad.
(.*) matches every single character until the end of the file name.
(And btw, .jpg means "any character" followed by jpg).
Teddy
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, May 11, 2005 10:08 PM
Subject: rename files
> Hi,
>
> I want to rename all the files in a directory with filenames starting
1.jpg
> to n.jpg.
> Why this code does not rename the files?
>
> opendir(DIR, "/tmp") or die "can not open dir: $!\n";
> my @files = grep {/.jpg/ } readdir(DIR);
> closedir(DIR);
>
> my $count = 1;
>
> foreach (@files){
> print "@files\n"; # works: prints all the files
> my $new = $_;
> $new =~ s/(.*)(.jpg)/$count$2/;
>
> rename($_, $new) or die "can not rename file: $!\n";
> print "$_ renamed to $new\n";
> $count++;
> }
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>