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]

In reply to Read contents of multiple files into new file and then move source files? by shadowfox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.