Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with a renaming script using Perl

Posted on 2013-10-30
2
Medium Priority
?
317 Views
Last Modified: 2013-10-31
I'm having trouble writing a script that will rename files in a directory based on a substitution.

For example, I have this and it works fine in terms of appending something to a filename:

use strict;
use warnings;
use File::Copy;


our @files = glob("*total*");      
foreach my $file(@files) {

my $newfile = $file . ".dat";

rename($file, $newfile)
   or die("Can't rename: $!\n");


}

So it will turn total.txt into total.txt.dat

However, when I change the script to this:

 use strict;
use warnings;
use File::Copy;


our @files = glob("*baz*");      
foreach my $file(@files) {

my $newfile = $file =~ s/total/CompleteTotal/;

rename($file, $newfile)
   or die("Can't rename: $!\n");


}

All I do is end up deleting the total.txt file instead of renaming the file CompleteTotal.txt; any ideas on the problem?
0
Comment
Question by:hadrons
[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
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 39612926
# my $newfile = $file =~ s/total/CompleteTotal/;
(my $newfile = $file) =~ s/total/CompleteTotal/;
0
 

Author Closing Comment

by:hadrons
ID: 39614166
Excellent
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

636 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