my $dir = 'C:/Documents and Settings/mydir/Desktop/current/Test_Files'; open my $out, '>', "$dir/data.txt" or die "can't open out file: $!"; opendir my $dh, $dir or die "can't opendir $dir : $!"; while(my $f = readdir($dh)) { next if ($f eq 'data.txt'); open my $fh, '<', "$dir/$f" or die "can't open file $f : $!"; my $first_line = <$fh>; while (my $line = <$fh>) { chomp $line; my ($well,$sample,$barcode,$block_id) = split(/\t/, $line); my $name = substr($block_id, 11); $sample =~ /(\d+)(.*)/; print $outfile "$well\t$1\t$2\$barcode\t$name\n"; } close $fh; } closedir $dh; close $out;