Solved

more elegant one?

Posted on 1998-07-01
6
234 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
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
Industry Leaders: 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!

 
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

Industry Leaders: 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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

685 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