kirk123 has asked for the wisdom of the Perl Monks concerning the following question:
I have a text file that contains a list of files that I need to update.
The list looks like the following:
My job is to open these files and search for the string "d25573786-.Log" and change it to "css1234.Log" If the file contain the string "d25573786-.Log" in its name to rename it also.Ex c:\\tmp\\d25573786-.Logc:\\tmp\\05102003.Log c:\\tmp\\05112003.Log c:\\tmp\\05122003.Log c:\\tmp\\05132003.Log c:\\tmp\\05142003.Log c:\\tmp\\05152003.Log c:\\tmp\\d25573786-.Log
Some how I can't rename the file. I can do it at the command line. Can someone tell me why my script is not working.
Here is a snippet of my code:
--thanks#!/usr/perl/bin #=============================================================# # # # #==============================================================# use strict; use File::Find; use Sys::Hostname; my $fileupdate; my $old_hostname = hostname(); #current hostname my ($orig,$concat,$new_hostname,$answer); my @lines = " "; my $new_hostname = ""; ## Create new hostname based on physical address ### $new_hostname = shift; if ($new_hostname eq "" ) { open(I,'ipconfig /all |'); while(<I>) { next unless /Physical Address/; /:/; chomp; tr/-//d; $new_hostname = "css" .(lc substr($_, -6,5)) ; last; } print "Current host name of machine is :$old_hostname \n"; print "The new host name is going to be: $new_hostname\n"; } if ( $old_hostname ne $new_hostname ) { &personalization; } else { print "System already personlized\n"; } #--------------------------------------------------------------------- +------------------------ sub personalization { open(FH,"<c:\\person.txt") || die "Can't open arp.txt: $!\n"; while(my $line = <FH>) { print "line $line\n"; if( $line =~ /(.*)$old_hostname(.*)/i ) # any files that cont +ain the substring of the old hostname { print "file that match $line\n"; $orig = $line; $concat = $line; $concat =~ s/(.*)$old_hostname(.*)/$1$new_hostname$2/gi; #CALL UPDATE SUBROUTINE &modified_file($concat); rename($orig, $concat) || print "error can't rename $ori +g to $concat: $!"; } else { &modified_file($orig ); } } } #--------------------------------------------------------------------- +------------------------ sub modified_file { $fileupdate = shift; open (IN,"<$fileupdate"); @lines = <IN>; close IN; my $change_count = 0; @lines = map { $change_count++ if s/$old_hostname/$new_hostn +ame/sgi; $_ } @lines; next unless $change_count; open (OUT,">$fileupdate") || "can't open $fileupdate for wri +ting :$! "; print OUT @lines; close OUT;
update (broquaint): added formatting + <readmore> tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Renaming files problem
by pzbagel (Chaplain) on May 21, 2003 at 00:23 UTC |