|
From: robertchen117 on 3 Feb 2007 22:36 I am trying to write a perl to replace all files under c:/perlbook/ like this: Perl 5 by Example Variables-ch03.htm rename to ch03-Perl 5 by Example Variables.htm But I really did not figure out, any one please help me? For some perl expert, it maybe only 5 minutes work. Thanks!
From: Bart Lateur on 4 Feb 2007 03:13 robertchen117(a)gmail.com wrote: >I am trying to write a perl to replace all files under c:/perlbook/ >like this: > >Perl 5 by Example Variables-ch03.htm rename to >ch03-Perl 5 by Example Variables.htm OK... chdir 'c:/perlbook' or die "Can't chdir: $!"; @files = glob '*.htm'; foreach my $old (@files) { (my $new = $old) =~ s/(.*)-(ch\d+)(?=\.htm$)/$2-$1/ or next; unless(-e $new) { rename $old, $new or warn "Can't rename file $old to $new: $!"; } else { warn "Can't rename file $old to $new: file name in use"; } } -- Bart.
From: Michele Dondi on 4 Feb 2007 06:28 On 3 Feb 2007 19:36:48 -0800, "robertchen117(a)gmail.com" <robertchen117(a)gmail.com> wrote: >I am trying to write a perl to replace all files under c:/perlbook/ >like this: > >Perl 5 by Example Variables-ch03.htm rename to >ch03-Perl 5 by Example Variables.htm $ perl -le 'rename $_,do {(my $n=$_)=~s/^(.*)-(ch\d\d)(?=\.htm$)/$2-$1/;$n } for <*.htm>' >But I really did not figure out, any one please help me? For some perl >expert, it maybe only 5 minutes work. Thanks! Should it take me 5 minutes I'd certainly do it by hand! ;-) Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
From: Tad McClellan on 4 Feb 2007 11:40 robertchen117(a)gmail.com <robertchen117(a)gmail.com> wrote: > Subject: rename *-ch03.htm to ch03-*.htm? You should put your question in the body of your message too. > I am trying to write a perl to replace all files under c:/perlbook/ > like this: > > Perl 5 by Example Variables-ch03.htm rename to > ch03-Perl 5 by Example Variables.htm > > But I really did not figure out, Show us what you have so far, and we will help you fix it. > any one please help me? Have a fish: ---------------------------------------------------------- #!/usr/bin/perl use warnings; use strict; use File::Find; my $dir = '.'; # my $dir = 'c:/perlbook'; find \&do_rename, $dir; sub do_rename { return unless (my $new = $_) =~ s/(.*)-(ch03)/$2-$1/; die "file $new already exists\n" if -e $new; rename $_, $new; } ---------------------------------------------------------- > For some perl > expert, it maybe only 5 minutes work. It would take more than 5 minutes to work out what you really want, so I gave only what you asked for instead. -- Tad McClellan SGML consulting tadmc(a)augustmail.com Perl programming Fort Worth, Texas
From: krakle on 4 Feb 2007 16:52
On Feb 4, 2:13 am, Bart Lateur <bart.lat...(a)pandora.be> wrote: > > (my $new = $old) =~ s/(.*)-(ch\d+)(?=\.htm$)/$2-$1/ or next; > Shouldnt you backslash a dash since a dash has a special meaning in a regex? (my $new = $old) =~ s/(.*)\-(ch\d+)(?=\.htm$)/$2\-$1/ or next; |