Hi:

I was able to use the code below to rename my files and directories recursively from XXXD25573786-2k2XX to XXX75219XX.

But I was unable to update the data in these files in the same manner.

I spend two days trying but can't get it right. How can I expand or modify the code below to update the files.

use File::Find; my $old_hostname= "D25573786-2k2"; my $new_hostname = "75219"; finddepth (\&update_file , "C:\/elmPaul" ); sub update_file_dir { if( /(.*)$old_hostname(.*)/i )# any files or directories { $orig = $File::Find::name ; $concat = $File::Find::dir . "\/$1$new_hostname$2"; rename($ori +g, $concat) || print "error can't rename $orig to $concat: $!"; } -f $_ && push (@files, "$File::Find::name"); } # end of sub update_file_dir foreach $file (@files ) { open (IN,"$file"); print "\nReading from file: $file \n"; @lines = <IN>; close IN; foreach (@lines) { if ( /(.*?)$old_hostname(.*?)/i ) { s/(.*?)$old_hostname(.*?)/$1$new_hostname$2/sgi; } open (OUT,">$file"); print OUT @lines; } }
If I ran the script the second time after the files and the directories had been renamed then it will update the files.

First time around the files and directories get rename and ONLY the file in "C:\elmPaul" get updated.

I also noticed that the script rename these files and directories from the bottom up. --kirk


In reply to Renames files and directories but won't update files at the same time. 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.