Hi,

I have a text file that contains a list of files that I need to update.

The list looks like the following:

c:\\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
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-.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:

#!/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;
--thanks

update (broquaint): added formatting + <readmore> tag


In reply to Renaming files problem by kirk123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.