#!/usr/bin/perl -- BEGIN { if ( eval { require Win32; 1 } ) { require ex::override; require Win32::Unicode::Native; ex::override->import( GLOBAL_stat => sub (;*) { &Win32::Unicode::Native::stat }, GLOBAL_lstat => sub (;*) { &Win32::Unicode::Native::stat } +, map({ my $name = $_; my $prototype = prototype("CORE::$name"); "GLOBAL_$name" => eval "sub($prototype){&Win32::Un +icode::Native::$name}"; } qw/ chdir link mkdir open readlink rename rmdir symlink unlink utime closedir opendir readdir /, ) ); } } use Path::Tiny qw/ path /; for my $drive ( @drives ){ my @picdirs = grep /^\d{4}_\d{2}_\d{2}$/, eval { path( $drive, $dir )->children }; if( @picdirs ){ ...; } } #### 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 Encode qw(encode decode); Main( @ARGV ); exit( 0 ); sub Main { #my( $infile_paths ) = @_; #if run via my( $infile_paths ) = 'C:\dev\test_path_names.txt'; chomp $infile_paths; my @paths = GetPaths( $infile_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 ) = shift; ## same as shift @_ ## my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #suffix for the backup-file, e.g. 2014-08-01 my $bak = "$date.bak"; my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main # Fix xml entities and create a copy of the original file before editing sub Replace { my( $in, $bak ) = @_; path( $in )-> copy( $bak ); #create a copy of $in with the ending(s) specified in $bak my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&}g; ## In some case does not match as intended s{\s>\s}{>}g; s{\s<\s}{<}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace