----- Original Message ----- From: <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Wednesday, May 11, 2005 2:23 PM
Subject: Re: rename files




----- Original Message ----- From: <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Wednesday, May 11, 2005 2: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?



More on above,
All the files are already named as 28.jpg, 59.jpg, 12.jpg, 10.jpg etc. but not in continuous order


After I run the script the, all the files are listed only if the (die "can not rename file: $!\n";) line is commented.
In the presense of the *die* line, the output is below: only 10.jpg shows up in printed files list.
------
10.jpg
can not rename file: No such file or directory


Thanks.

Terminated with exit code 2.

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/;

Thanks Teddy, but after changing this >> $new =~ s/(.*)(.jpg)/$count$2/; to $new =~ s/(\d+)(.jpg)/$count$2/; does not solve it either.??

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>




Reply via email to