use strict;
use warnings;
use File::Copy;

my $inFile;

opendir(my $dh, ".") || die "can't opendir .: $!";
my @files = grep(/\.dat$/,readdir($dh));

foreach my $d (@files) {
  copy($d,$d.".bak");
  open($inFile, "<",$d.".bak");
  open(my $outFile, ">",$d);
  
  while (<$inFile>) {
    s/(;EPrints.*\.).*?(;EPrints)/$1agg$2/;
    print $outFile $_;
  }
}

closedir $dh;