Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Batch processing files in a directory

by 7stud (Deacon)
on Feb 13, 2013 at 17:58 UTC ( #1018591=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018591]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-02-22 17:02 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (295 votes). Check out past polls.