kirk123 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Renames files and directories but won't update files at the same time.
by fokat (Deacon) on Aug 24, 2002 at 00:32 UTC | |
by Arien (Pilgrim) on Aug 27, 2002 at 12:46 UTC | |
|
Re: Renames files and directories but won't update files at the same time.
by graff (Chancellor) on Aug 27, 2002 at 03:59 UTC | |
|
Re: Renames files and directories but won't update files at the same time.
by bart (Canon) on Aug 27, 2002 at 22:07 UTC | |
|
Re: Renames files and directories but won't update files at the same time.
by thealienz1 (Pilgrim) on Aug 27, 2002 at 15:01 UTC |