http://www.perlmonks.org?node_id=1018591


in reply to Batch processing files in a directory

I've been reading about opendir and readdir as well, so I don't know what my starting point should be.

opendir() will return all the files in a directory--but you only want the .ptseq files, so why start with a bigger set of files than you want? I recommend globbing as your starting point. However, when globbing don't use the line input operator(<>)--use glob() instead.

use strict; use warnings; use 5.010; use File::Basename; my $target_file_ext = '.ptseq'; my $target_file_pattern = "*$target_file_ext"; for my $in_name (glob $target_file_pattern) { my ($name) = fileparse($in_name, ($target_file_ext) ); my $out_1_name = "${name}1.txt"; my $out_2_name = "${name}2.txt"; open my $OUT_1, '>', $out_1_name or die "Couldn't open $out_1_name: $!"; open my $OUT_2, '>', $out_2_name or die "Couldn't open $out_2_name: $!"; open my $INFILE, '<', $in_name or die "Couldn't open $in_name: $!"; while (my $line = <$INFILE>) { chomp $line; #Some $line processing here: say {$OUT_1} "$line ($out_1_name)"; say {$OUT_2} "$line ($out_2_name)"; } close $INFILE; close $OUT_1; close $OUT_2; }
Also, note that filenames can look like this: one.two.three.ptseq (which my not apply in your case), so I used File::Basename to extract the filename without the extension.