use Cwd 'abs_path'; $dir=abs_path($0); $dir=~s/\\html.pl//g; opendir (DIR, $dir) or die $!; while ($fileName = readdir(DIR)) { if ($fileName=~/\.html/i) { $pathName="$dir\\$fileName"; $pathName=substr($pathName,0,length($pathName)-4); open(DATA, "+<${pathName}html") or die "Couldn't open file file.txt, $!"; #open(OUT, ">${pathName}html") or die "Couldn't open file file.txt, $!"; while() { $line.=$_; } . . . . print OUT $line; } } close (DIR);