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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Append Text Files
by drodinthe559 (Monk) on Oct 09, 2009 at 17:37 UTC | |
by BioLion (Curate) on Oct 09, 2009 at 17:44 UTC | |
by drodinthe559 (Monk) on Oct 09, 2009 at 17:52 UTC | |
by drodinthe559 (Monk) on Oct 09, 2009 at 19:01 UTC | |
by BioLion (Curate) on Oct 10, 2009 at 10:56 UTC |