Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming Files with Perl

Status
Not open for further replies.
Apr 17, 2006
27
0
0
US
I have 2 files on disk - oldfilename1.bak and oldfilename2.bak

My goal is to rename oldfilename1.bak to newfilename1.bak, oldfilename2.bak to newfilename2.bak

The old file names are held in a text file called - old.txt, contents ( one on each line) :
oldfilename1
oldfilename2

The new file names are held in a text file called - new.txt, contents (one on each line) :
newfilename1
newfilename2

I'm using this script, but it doesn't work. I think it is because there's a new line when I call the value from the array, can someone please give me any pointers? When I print the array, it looks okay.

*******

#!/usr/bin/perl

open (old,"old.txt") || die ("There was an error opening file: $!");
@oldnames = <old>;
close (old);

open (new,"new.txt") || die ("There was an error opening file: $!");
@newnames = <new> ;
close (new);

for $i (0 .. $#oldnames)
{
$a = "$oldnames[$i]";
$b = "$newnames[$i]";
rename("$a.bak","$b.bak");
}

*************

Thank you, very much.
 
mustaine737,
Yes, you are right. The problem is because of new line characted at the end of array element.
Remove it using chomp and it should be fine. Also try avoiding $a and $b names for varaibles as they are used for some special purpose.

Code:
for $i (0 .. $#oldnames)
{
$old = "$oldnames[$i]";
$new = "$newnames[$i]";
chomp($old);
chomp($new);
rename("$old.bak","$new.bak");
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 

does this help. it reads old.txt file line by line and prints them to new.txt file.

#!/usr/bin/perl -w
open(FILER1, "old.txt") || die "couldn't open the file\n";
open(FILEW1, ">new.txt") || die "couldn't create the file\n";
while(!eof(FILER1)){
my $line=<FILER1>;
$line=~s/oldfilename/newfilename/;
print FILEW1 "$line";
}
 
Having the data in two separate files is a bit risky - the files have to be kept in sync and one missing line could screw up all your renames. It would be safer to have a single file with pairs of filenames on each line...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve's got a point. It would be a good idea to at least check to make sure you have the same number of entries in each list.
 
All,

Thank you very much for your replies :)

I chomped the array and also added some logic to ensure that the file names are in sync.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top