Solved

Help with a renaming script using Perl

Posted on 2013-10-30
2
307 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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