how to use Perl to rename the filenames and directory names under current and subdirectories recursi

L

lucy

Is there a way to use Perl command line

to rename all the filenames and directory names

from '*abcd*.*' to '*xyz*.*'

and do this recursively for all files and all subdirectories?

Thanks a lot
 
A

A. Sinan Unur

lucy said:
Is there a way to use Perl command line

to rename all the filenames and directory names

from '*abcd*.*' to '*xyz*.*'

and do this recursively for all files and all subdirectories?


use File::Find;
 
A

Anno Siegel

lucy said:
Is there a way to use Perl command line

to rename all the filenames and directory names

from '*abcd*.*' to '*xyz*.*'

and do this recursively for all files and all subdirectories?

See the _Perl Cookbook_, a section titled "Renaming Files".

Anno
 
M

Michele Dondi

^^^^^^^^^^^^^^^



use File::Find;

With some caution because of the above detail, i.e. if she really
wants to also rename directory names (and there are some to be
renamed), then something along the lines of

find sub {
rename $_, do {
(my $n=$_) =~ s/abc/xyz/ or return;
$n;
}
}, '.';

which is the kind of solution one may naively think about, simply
won't work. IMHO the best workaround would be to do the renaming in
'preprocess', returning the updated list, and actually doing nothing
in 'wanted'. All this is left as an exercise to the OP... ;-)


Michele
 
J

Jürgen Exner

Michele said:
^^^^^^^^^^^^^^^



use File::Find;

With some caution because of the above detail, i.e. if she really
wants to also rename directory names (and there are some to be
renamed), [...]
which is the kind of solution one may naively think about, simply
won't work. IMHO the best workaround would be to do the renaming in
'preprocess', returning the updated list, and actually doing nothing
in 'wanted'. All this is left as an exercise to the OP... ;-)

Way to complicated. Just to a depth-first parsing and you will be fine.

jue
 
M

Michele Dondi

Way to complicated. Just to a depth-first parsing and you will be fine.

D'Oh! You're *perfectly* right... ;-)

....Though I wouldn't say "way too complicated" (just "more
complicated"). It would be a matter of something similar to:

find { preprocess =>
sub {
map {
my $o=$_;
rename $o, $_ or
warn "Can't rename `$o' to `$_': $!\n"
if s/abc/xyz/;
$_;
} @_;
},
wanted => sub {} }, '.';

BTW, I must say that I have tried to avoid the 'wanted' line and I got
an error, reasonably enough in terms of UI for logically it is
expected that one would use the 'wanted' action on wanted files.

Anyway I wonder wether it could be acceptable/desirable to have a
default implicit C<<wanted => sub {}>> instead...


Michele
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top