rename *-ch03.htm to ch03-*.htm?

R

robertchen117

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

John Bokma

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?

read all items in the dir (readdir) and grep the ones that
are files ending in htm

for each file:
rename it


Show code you have written so far.

For some perl expert, it maybe only 5 minutes work.

A sentence like this might piss of a lot of people. When people are
willing to pay me for it, it already does piss me off. They lack the
expertise to code it themselves, yet they know how much time it will take
me.

Renaming it manually will take you less then 5 minutes.
 
B

Bart Lateur

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";
}
}
 
T

Tad McClellan

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

krakle

(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;
 
P

Paul Lalli

Shouldnt you backslash a dash since a dash has a special meaning in a
regex?

Says who?

A dash has a special meaning within a character class, but not within
a regex itself.

perldoc perlretut
perldoc perlre
perldoc perlreref

Paul Lalli
 
D

Dr.Ruud

Michele Dondi schreef:
krakle:

No, because it hasn't. Except in charachter classes, that is.

And not even there if you put it first (after any modifier like ^) or
last.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top