I wrote some code for you below. I didn't completely test it but it does illustrate some basic ideas.
1. A loop like this, which is common in 'C' is seldom needed in Perl: for ($i=1; $i < 119000; $i++) because we have a foreach(@xyz){} iterator that "visits" all elements of @xyz without having to know the number in advance.
2. There are a number of ways to get the files within a directory that match a pattern. Below I show the way needed with Active State Perl 5.6 within comments, but if you have say Perl 5.10 the glob() method below will work fine.(there are at least 3 variants of glob that I know of).
3. The way that is the most safe when modifying a file is make a temp file, do your thing and then if all works ok, delete the original file and replace with the new file. There are actually even more safe ways than I've shown here for that. But this is good for 99% of cases.
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 (<IN>)
{
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 "$!";
}
|