I am processing a large directory every night. It accumulates around 1 million files each night, half of which are .txt
files that I need to move to a different directory according to their contents.
Each .txt
file is pipe-delimited and contains only 20 records. Record 6 is the one that contains the information I need to determine which directory to move the file to.
Example Record:
A|CHNL_ID|4
In this case the file would be moved to /out/4
.
This script is processing at a rate of 80,000 files per hour.
Are there any recommendations on how I could speed this up?
opendir(DIR, $dir) or die "$!\n";
while ( defined( my $txtFile = readdir DIR ) ) {
next if( $txtFile !~ /.txt$/ );
$cnt++;
local $/;
open my $fh, '<', $txtFile or die $!, $/;
my $data = <$fh>;
my ($channel) = $data =~ /A\|CHNL_ID\|(\d+)/i;
close($fh);
move ($txtFile, "$outDir/$channel") or die $!, $/;
}
closedir(DIR);