Greetings perl masters!
I am a beginner with perl and I would like to seek your help regarding my script.
My script should find a string within the files then copy it to a directory. When I test copy within my local dirs, the script works. But when I tried to copy from a source dir (which is linux), the script doesn't work.
Please help! Thanks!
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Copy;
my $target = 'C:\target';
my $pattern = 'SearchWord';
open my $copylog, '>', 'C:\target\copylog.csv' or die "Error opening c
+opylog.csv: $!\n";
open STDERR, ">>&=", $copylog or die "Can't redirect STDERR";
$copylog->autoflush(1);
find( sub {
if (-f) {
open my $file, '<', $File::Find::name or die "Error opening fi
+le: $!\n";
while (my $line = <$file>) {
if($line =~ m/$pattern/ig) {
copy ($File::Find::name, $target) or die "Copy fai
+led: $!";
print $copylog $_ . "," . $pattern . "\n";
}
}
close $file;
}
},glob ('Z:\Linux\site'));
Update:
I had used Loops sample and it worked:
use strict;
use warnings;
use File::Find;
use File::Copy;
use autodie;
my $log = q(C:\target\copylog.csv);
my $target = q(C:\target);
my $pattern = qr(SearchWord);
close *STDERR; open *STDERR, '>', $log;
find(
sub {
if (-f) {
open my $file, '<', $_;
while (my $line = <$file>) {
if($line =~ m/$pattern/ig) {
copy ($_, $target);
warn "$_, $pattern\n";
last;
}
}
close $file;
}
},
glob ('Z:\Linux\site')
);
It seems that copying from a drive with other platform should not be an issue. It is a matter of faulty script that does not make it work. Again, thank you guys for the time spent in dealing with my issue. All pointers are taken and I will apply it in my scripting.
Thanks and best regards!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.