I am currently trying to collect every line of certain files within sub directories and output them all into a single large file... this works great, but it has to re-append ALL files every time I add a new sub directory.

What I'd like:

When running the script, it checks to see if it has already taken the data out of each file, if it has, it skips that file, and moves to the next. When it finds a file that has not been appended, it will append to the existing large file.

What I have tried:

I have the script generating a second file containing all file paths that it appends, then it's supposed to open that file the next time it runs, it should see that a file path is in there, and then not append that data to the large file again. The problem is it either appends everything again every time, or it appends all data the first time, and nothing (even new stuff) the second time.

I attempted all the styles of array checking found in the "How to find out if X is an element in an array?" Q&A, but none will work. where the current file path is the element, and the array filled by reading the file paths out of the second folder.

What I think the problem is:

The array is filled, but my if statement is not functioning correctly, it needs to check if the new file path is the same as ANY in the second file(my array), if it is, then it does not append the data again, if, after checking ALL elements of the array the file path does not match, then it appends and adds the new file path to the second file.

if (grep { -f $_ and $_ eq $FilePath } @DirsArray){} else{ open (FILE, $FilePath ) or die "Cannot open file: $!"; printf("\n" . $FilePath . "\n"); while ( $line = <FILE> ) { push(@outLines, $line); #to be sent to appended large file } close FILE; print (LASTFILES $FilePath."\n"); #prints to second file }
Any ideas why its not matching the array with the file path (also, I tried pretty much all the ways listed on that Q&A page, not just this version)?

Thanks,

Josh


In reply to Append new files only. by c4jjm

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.