Solved

more elegant one?

Posted on 1998-07-01
6
203 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now