in reply to write multiple files to a new directory
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!: $!"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: write multiple files to a new directory
by Anonymous Monk on Oct 23, 2015 at 22:20 UTC |