Solved

more elegant one?

Posted on 1998-07-01
6
227 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

765 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