shadowfox has asked for the wisdom of the Perl Monks concerning the following question:
I know there's going to be many approachs to this but using my inital approach I'm not getting the desired results, atleast not entirely.
I want to take a folder, which is typically thousands of files, but they are small files, maybe 1-5 files at most. So I used glob to grab all the .txt files, and then write out the data from all the files into 1 new combined file in another folder. That works ok, 3000 files are read into a new file in about 10 seconds.
My problem comes next where I want to take the files that were read and move them to an archive folder, new files are written to the source folder all the time so I naturally don't want to read the same files over again or move any that were not read yet, so it needs to be the same array of files, for lack of a better grouping term.
In the context of the code below, the move command is only moving the very first file, it does read all files but its only moving the one so I've cleared missed something that's hopfully not terribly obvious. I thought about switching over to readdir for file handle operations, but I wanted to confirm this will/won't work first.
As always thanks in advance for any and all help provided!
# combinedinput.pl use warnings ; use strict ; use File::Copy ; my $INDIR = "C:/output/input"; my $OUTDIR = "C:/output/input/archive"; my $NEWFLE = "COMBINED.output"; my $COUNT = "0"; my $FILE =""; print "[Task started on " . scalar localtime . "]\n"; chdir($INDIR); open (my $FH, '>', "../$NEWFLE") or die "etc: $!"; @ARGV = glob('*.txt'); foreach $FILE (@ARGV) { while (<>) { print $FH $_; $COUNT++; } move($FILE, $OUTDIR) or die "Could not move $FILE to $OUTDIR: $!\n +"; } close ($FH); print "\n"; print " Found $COUNT files with data to process \n"; print " Reading all file data into memory \n"; print " $NEWFLE generated successfully \n"; print "\n"; print "[Task completed in "; print time - $^T . " seconds] \n" ; #END
<output results> [Task started on Fri Feb 10 13:49:17 2012] Found 3012 files with data to process Reading all file data into memory COMBINED.output generated successfully [Task completed in 11 seconds]
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Read contents of multiple files into new file and then move source files?
by Eliya (Vicar) on Feb 10, 2012 at 19:23 UTC | |
by shadowfox (Beadle) on Feb 10, 2012 at 19:56 UTC | |
Re: Read contents of multiple files into new file and then move source files?
by admiral_grinder (Pilgrim) on Feb 10, 2012 at 19:50 UTC | |
Re: Read contents of multiple files into new file and then move source files?
by jdrago999 (Pilgrim) on Feb 10, 2012 at 19:42 UTC | |
by aaron_baugher (Curate) on Feb 10, 2012 at 23:20 UTC | |
by jdrago999 (Pilgrim) on Feb 11, 2012 at 00:41 UTC |