Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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.


Comment on Re: Batch processing files in a directory
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-10-22 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls