use 5.014; use strict; use warnings; use File::Slurp qw(:all); use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; my $infile_paths = 'C:\temp\test_folder\paths.txt'; my @paths = read_file $infile_paths, { binmode => ':utf8' } or die $!; chomp(@paths); # say for @paths; foreach my $path (@paths){ Main($path), } Main( @ARGV ); exit( 0 ); sub Main { my $directory = ""; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); my $bak = "$date.bak"; my @xml_files = path( @paths )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main sub Replace { my( $in, $bak ) = @_; path( $in )->move( $bak ); 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