in reply to Read contents of multiple files into new file and then move source files?

here is an approach using a few more modules from CPAN. I think it would make it more expandable in the future or readable 6 months from now.
use warnings ; use strict ; use File::Copy ; use Path::Class ; # file(), dir() use File::Find::Rule ; my $INDIR = "C:/output/input"; my $OUTDIR = "C:/output/input/archive"; my $NEWFLE = "COMBINED.output"; my $COUNT = 0; print "[Task started on " . scalar localtime . "]\n"; my $new_file = file( $INDIR, '..', $NEWFLE ); $new_file->touch(); my $FH = $new_file->openw() or die "etc: $!"; my @files = File::Find::Rule->file()->name( qr/.*\.txt/i )->in( $INDIR + ); @files = map{ file( $_ ) } @files; foreach my $file ( @files ) { # Skip file if it is in the archive path. my $archive_path = file( $outdir, $file->basename() ); next if -e $archive_path; # Process file $COUNT++; my @lines = $file->slurp(); print $FH $_ for @lines; # you have to "stringify" Path::Class objects passed to File::Copy move( "$file", "$archive_path" ) or die "Could not move $file to $OU +TDIR: $!\n"; } $FH->close(); print "\n"; print " Found $COUNT files with data to process \n"; print " Reading all file data into memory \n"; print " ${new_file} generated successfully \n"; print "\n"; print "[Task completed in "; print time - $^T . " seconds] \n" ;
  • Comment on Re: Read contents of multiple files into new file and then move source files?
  • Download Code