Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Batch processing files in a directory

by 7stud (Deacon)
on Feb 13, 2013 at 17:58 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found