Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Append Text Files

by drodinthe559 (Monk)
on Oct 09, 2009 at 17:17 UTC ( #800344=perlquestion: print w/ replies, xml ) Need Help??
drodinthe559 has asked for the wisdom of the Perl Monks concerning the following question:

Guru's, I need to append 13 text files into one. The file name and directory will always be static and remain the same. I tried the file copy command, but it just over wrote the previous file.
#!/usr/bin/perl -w use strict; use warnings; use File::Copy; open(LOGFILE, ">> C:/Logs/RPT_APPEND.log"); my $targetfile = "C:/testrpt.txt"; my $procdate = sprintf "%04d%02d%02d %02d:%02d:%02d", (localtime)[5]+1 +900, (localtime)[4]+1, (localtime)[3], (localtime)[2], (localtime)[1] +, (localtime)[0]; my @rpts = glob('//testserver/dir/*'); foreach my $x (@rpts) { print $x . "\n"; copy($x, $targetfile); }
Help would be appreciated. Dave R.

Comment on Append Text Files
Download Code
Re: Append Text Files
by BioLion (Curate) on Oct 09, 2009 at 17:24 UTC

    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...
      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...
Re: Append Text Files
by bv (Friar) on Oct 09, 2009 at 17:46 UTC
    #!/usr/bin/perl use strict; use warnings; system('for %f in (\\\\testserver\\dir\\*) do echo %f >> testrpt.txt & +& type %f >> testrpt.txt');
    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
Re: Append Text Files
by NetWallah (Abbot) on Oct 09, 2009 at 20:51 UTC
    Here is a BAT file that should do the job for you:
    setlocal ENABLEDELAYEDEXPANSION Set SOURCELIST=X for /f %%f in ('dir /b \\testserver\dir\*') do set SOURCELIST=%%f+!SOU +RCELIST! set SOURCELIST=%SOURCELIST:~0,-2% echo ==%SOURCELIST%== copy %SOURCELIST% dest\testrpt.txt

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Append Text Files
by llancet (Friar) on Oct 10, 2009 at 07:36 UTC
    very simple
    # suppose we have input file names in @file_in # and output file name in $file_out open OUT,">$file_out"; foreach my $current (@file_in) { open IN,$current; print OUT while <IN>; close IN; } close OUT;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://800344]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-29 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls