in reply to Re^2: Performing a search and replace in XML across multiple directories
in thread Performing a search and replace in XML across multiple directories

OK, I have updated the script to this.. It renames the file corrctly but I get no output in the renamed file.
use File::Find; use strict; my $bak_ext = '.bak'; my $root_dir = 'C:/Temp/Chris/'; find(\&fix, $root_dir); sub fix { # if the extension fits... if ( /\.xml?/i ) { print "Processing $_\n"; my $new = $_; my $bak = $_ . $bak_ext; rename $_, $bak or die "Cannot rename $_ to $bak: $!"; open OUTPUT, "> $new" or die "Cannot open $new for writing: $! +"; while ( <OUTPUT> ) { Print Go Go Go; my $TheLine = $_; s/<label>([^<]*<\/label>)/<label denominator=no auto.number=no>$1/i; print OUTPUT; } } close OUTPUT; }
  • Comment on Re^3: Performing a search and replace in XML across multiple directories
  • Download Code

Replies are listed 'Best First'.
Re^4: Performing a search and replace in XML across multiple directories
by GrandFather (Saint) on May 16, 2006 at 23:43 UTC

    What are you expecting while (<OUTPUT>) to do?

    Looks like what you really want is an in place edit. Someting like this may get you to where you want to be:

    use strict; use warnings; use File::Find; my $bak_ext = '.bak'; my $root_dir = 'C:/Temp/Chris/'; find(\&fix, $root_dir); sub fix { # if the extension fits... return if ! /\.xml?/i; print "Processing $_\n"; # set up in place edit local @ARGV = ($_); # local $^I = $bak_ext; # Edit the file while (<>) { # Edit $_ (the current line) as required here print; # Write out the new version of the line } }

    DWIM is Perl's answer to Gödel