use strict; use warnings; use File::Copy; my $destination = "/directory/originalinputfiles/"; my @files = glob ("/directoy/*.txt"); foreach my $file (@files) { my $newfile = $file.".tmp"; open (IN,'<:encoding(UTF-8)', $file) or die "Could not open '$file' $!"; open (OUT,'>:encoding(UTF-8)', $newfile) or die "Could not open '$newfile' $!"; while (my $text = ) { $text =~ s/^[a-z].*[a-z]$//gmi; print OUT $text; } close (IN) or die "Could not close input file: '$file' $!"; close (OUT) or die "Could not close output file '$newfile' $!"; move $file, $destination or die "Could move the orignal input file: $file to the folder: '$destination' $!"; rename $newfile, $file or die "Could not rename file: '$newfile' $!"; print "\n done \n"; }