#!/usr/bin/perl -- use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; Main( @ARGV ); exit( 0 ); sub Main { #~ my( $infile_paths ) = @_; my( $infile_paths ) = 'C:\temp\test_folder\paths.txt';; my @paths = GetPaths( $infile_paths ); for my $path ( @paths ){ FormerlyMainNowReplaceXmlKids( $path ); } } ## end sub Main sub GetPaths { my @paths = path( shift )->lines_utf8; s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub FormerlyMainNowReplaceXmlKids { #~ my( $directory ) = @_; ## same as shift @_ ## same as shift my $directory = shift; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); my $bak = "$date.bak"; my @xml_files = path( $directory )->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