Welcome to the Monastery, jingmcgill!

First things first, whenever you post code, input data, or expected output data, please put it into <code></code> tags per Markup in the Monastery. With that out of the way, let's carry on...

You have some syntax issues with the code you've supplied. The first two lines in your Perl programs should always be use warnings; and use strict;. These two pragmas will catch almost all issues that prevent a program from compiling/running.

Next, you should always use the three-argument form of open, and catch any errors if they present themselves with a die statement.

The below example shows everything I've mentioned above in action, and after you've replaced my file paths with your original ones, you should be good to go. Note to actually copy the file(s), I've used the copy() function from the File::Copy module, which is built into the perl core.

use warnings; use strict; use File::Copy; my $write_dir = 'two/'; my @files = <one/*>; for my $file (@files){ open my $fh, '<', $file or die "can't open file $file for reading!: $!"; my $copy_it = 0; while (my $line = <$fh>){ if ($line =~ /keyword/){ print "$file\n"; $copy_it = 1; last; } } close $fh; if ($copy_it){ copy $file, $write_dir or die "can't copy file $file to $write_dir!: $!"; } }

In reply to Re: write multiple files to a new directory by stevieb
in thread write multiple files to a new directory by jingmcgill

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.