use warnings; use strict; # is one way to get the file names # I think here we can just use glob() if you are at Perl 5.10 # my $source_dir = "C:/convs"; # opendir (DIR, $source_dir) || die "unable to open $source_dir $!"; # @files = grep{m/conv\d+\.txt/}readdir DIR; my $source_dir = "C:/convs"; my @files = glob("$source_dir/conv*.txt"); foreach my $file (@files) { open (IN, "$source_dir/$file") || die "unable to open $source_dir/$file $!"; open (TEMP, "$source_dir/$file.tmp" || die "unable to open $source_dir/$file.tmp $!; while () { s/^.*Mina olen.{1561}//s; #/s allows "." to match newline #I'm not sure that it is needed here. print TEMP $_; } close TEMP || die "$!"; #unlikely to fail (file is "open") close IN || die "$!"; unlink ("$source_dir/$file") || die "$!"; rename ("$source_dir/$file.tmp", "$source_dir/$file") || die "$!"; }