Solved

more elegant one?

Posted on 1998-07-01
6
242 Views
Last Modified: 2010-03-04
hello,
a perl code should find in specified directory recursively all the file ended with given suffix1  and rename it to file ended with given suffix2.(behave like mv *.aa *.bb)  


$suffix1='.aa';
$suffix2='.bb';

@f=`find $path -type f -name '*$suffix1'`;

foreach $x (@f){
 chop($x);#get rid of \n
 $tem=$x;
 $tem=~ s/$suffix1$/$suffix2/;

  `mv $x $tem`;

}

I wonder whether there is better way to implement it.
0
Comment
Question by:tian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:bertvermeerbergen
ID: 1208257
Instead of running shell commands (launching a seperate shell every time), you could use the rename function and the File::Find library module

Example:

#-- The subroutine called by find for every entry
sub DoSomething
{
  #-- $File::Find::dir contains the current directory (chdir() has been done)
  #-- $_ contains the current file or subdir name
  #-- $File::Find::name is "$File::Find::dir/$_"

  here, you check for matching entry and then use perl rename function

}

#-- (this is in your main or some other subroutine)
#-- Parameters:
#--   1: A reference to the subroutine to be called for every item
#--   2: The top-level directory you want to scan
find(\&DoSomething, $path);
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 1208258
$suffix1='.aa';
$suffix2='.bb';

@f=`find $path -type f -name '*$suffix1'|sed -e 's/\(.*\)$suffix1/mv & \1$suffix2|sh`;


# works, even without perl ;-)
0
 

Author Comment

by:tian
ID: 1208259
Thanks  ahoffmann. Before I close this question, can someone provide me an implementation based on bertvermeerbergen's idea?

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 84

Expert Comment

by:ozo
ID: 1208260
use File::Find;
find(\&DoSomething, $path);
sub DoSomething {
 my $tem;
 -f && ($tem=$_)=~s/\Q$suffix1\E$/$suffix2/ && rename($_,$tem);
}

0
 

Author Comment

by:tian
ID: 1208261
thank you
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208262
tian, are you shure that the points are for my and not for ozo's suggestion?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question