Solved

Help with a renaming script using Perl

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Choosing the right language for new project 8 66
rhino JavaScript import, load 25 108
Problem to error 4 74
Problem to App 4 119
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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