On 5/11/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> 
> ----- Original Message -----
> From: "bright true" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Cc: <beginners@perl.org>
> Sent: Wednesday, May 11, 2005 2:55 PM
> Subject: Re: rename files
> 
> >The following would rename any file in a directory (.anything) into numbers
> >
> >in the same extention i hope it will be usfull
> >
> >opendir(DIR,"$dir") or print "$!";
> >my @content = sort(readdir(DIR));
> >closedir(DIR);
> >my $no=0;
> >*foreach my* $number (@content){
> >$no++;
> >$number =~m/\S+\.(\S+)/;
> >rename("$dir/$number","$dir/$no.$1");}
> 
> >by the way , you forgot to mention the directory you want to rename file in
> 
> Exactly, Right. Thanks, Bright.
> 
> 
> >therefore you got this Error : can not rename file: No such file or
> >directory
> 
> >rename("$dir/$file","$dir/$file");
> 
> >bye

One more thing to keep in mind: if you want the files to stay in order
(i.e. 3.jpg should always come out with a lower number than 56.jpg,
even if there are gaps), you need to sort the array.  readdir() will
will return the files in an OS specific order.  On unix systems it
will probably be asciibetical to mimic ls, i.e. 1, 10, 100, 2, 20,
200...  So if you start with, say, 3.jpg, 56.jpg and 150.jpg, you're
may end up with:

1.jpg => 150.jpg
2.jpg => 3.jpg
3.jpg => 56.jpg

You probably want a 'sort {$a <=> $b} @files' in there somewhere.

HTH,

--jay

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