#!/usr/bin/perl -w use strict; use CAM::PDF; use CAM::PDF::PageText; use File::Spec::Functions; use File::Find::Rule; ## ... steps 1 and 2 as per OP code ... my $outputDir = 'F:/project_italia/firm_text'; ( -d $outputDir and -w _ ) or die "Output directory $outputDir not found or not writable\n"; open(LIST, ">>", 'F:/project_italia/firmList_pdf.txt' ) or die "Can't open destination.fil $!"; foreach (@allDir) { ... foreach (@filings) { my $fileName=$_; my $pdf = CAM::PDF->new($_) or do { warn "PDF->new failed on $_\n"; next; }; ... # open output text file (NB: ">>" appends, creates if necessary) open(DEST, ">>", "$outputDir/$textName[4].txt" ) or do { warn "Unable to append to $outputDir/$textName[4].txt: $!\n "; next; }; ...