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.
If I ran the script the second time after the files and the directories had been renamed then it will 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; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |