use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; use File::BOM; use Carp::Always; use Data::Dump qw/ dd /; use Win32; use Win32::Unicode qw/ statW /; Main( @ARGV ); exit( 0 ); sub Main { #my( $infile_paths ) = @_; my( $infile_paths ) = 'C:\dev\test_paths.txt';; my @paths = GetPaths( $infile_paths ); #print "The following paths were in the file:\n"; #say for @paths; for my $path ( @paths ){ RetrieveAndBackupXML( $path ); } #return @paths; } ## end sub Main sub GetPaths { use File::BOM; ## my @paths = path( shift )->lines_utf8; my @paths = path( shift )->lines( { binmode => ":via(File::BOM)" } ); s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub RetrieveAndBackupXML { #~ my( $directory ) = @_; ## same as shift @_ ## same as shift my $directory = shift; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #sets current date and time to be added to backup file my $bak = "$date.bak"; #date added to the .bak file my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); #sub Replace using it's 2 parameters as defined below } } ## end sub Main sub Replace { my( $in, $bak ) = shift; path( $in )->move( $bak ); #Creates a copy of the original file my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { #s{&}{&}g; ## will match more than what you want fix it s{&amp;}{&}g; s{\s>\s}{>}g; s{\s<\s}{<}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace