in reply to Append Text Files

You can append the files into the merged document, just like you are appending to the log file:

use strict; use warnings; ## open the 'merge' for writing ## this way you will only wrote in the files from this merge ## and not keep anything that is already in there ## to keep any pre-existing content, leave as # open(my $merge_fh, '>>', $targetfile) || die "Failed to append to ta +rget: $!"; ## always check for success on open/close FH operations open(my $merge_fh, '>', $targetfile) || die "Failed open \'$targetfile\' for merging: $!"; my @rpts = glob('//testserver/dir/*'); foreach my $x (@rpts) { ## open each file to be merged in turn open (my $fh, '<', $x) || die "Failed open \'$x\' for reading : $! +"; ## read out each line, printing it into the target file while (<$fh>){ print $merge_fh $_;} ## close up close $fh || die "Failed to close \'$x\' : $!"; } ## now all the files have been copied into the target, you are free to + close it. close $merge_fh || die "Failed to close $targetfile after merge : $!";

Update: Improved code

Just a something something...

Replies are listed 'Best First'.
Re^2: Append Text Files
by drodinthe559 (Monk) on Oct 09, 2009 at 17:37 UTC
    That worked BioLion. I don't quite understand the purpose of the second line from the bottom. It appears it goes through each line. Can you explain.
    while (<$fh>){ print $merge $_;}
    Thanks for your help. Dave

      It is! Basically you are just reading in each of the files to be merged, and printing out each line to the 'merge file'. This is probbaly not the most elegant way of doing this, but it gets the job done.

      I'll add some comments to my rush code to make things clearer...

      Just a something something...
        I thought so but wasn't sure. I know its pretty easy to do with a Windows batch file. If you know how to make it more elegant, let me know.
        It is clearer, but I'd like to do something similar to piping in using cat.