.... ### my $newdir= 'c:\redone'; my $newdir= 'c:/redone'; my $extension= ".txt"; ## I would be tempted to take the preceeding 2 statements out of the loop my $target = "$newdir/$line"; open( FINAL, ">$target" ) or die "Can't open $target:$!"; #output database results ### while (@row=$sth->fetchrow_array) { while (@row=$sth->fetchrow_array()) { ...