Solved

Help with a renaming script using Perl

Posted on 2013-10-30
2
300 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
Comment Utility
# my $newfile = $file =~ s/total/CompleteTotal/;
(my $newfile = $file) =~ s/total/CompleteTotal/;
0
 

Author Closing Comment

by:hadrons
Comment Utility
Excellent
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

16 Experts available now in Live!

Get 1:1 Help Now