Solved

Help with a renaming script using Perl

Posted on 2013-10-30
2
309 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
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 257
Formatting stings with pack and printf in perl 5 78
Perl Frameworks 1 98
C# code editing and collaboration 3 136
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

827 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