use Cwd 'abs_path'; $dir=abs_path($0); opendir (DIR, $dir) or die $!; my @html_files = grep(/\.html$/, readdir DIR); closedir DIR; foreach my $html_files(@html_files) { $pathName=substr($html_files,0,length($html_files)-4); open(DATA, "<${pathName}html") or die "Couldn't open file file.txt, $!"; while() { $line.=$_; } . . . open(DATA, ">${pathName}html") or die "Couldn't open file file.txt, $!"; print DATA $line; $line=""; } closedir(DIR);