Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi bbb, welcome to the monastery.

Not sure what error you're hitting without having more information as the monks above mentioned. But thought a few small touches to your script, might get you closer to you finding the answer.

Tried to keep the bulk of your code, but did switch over to using autodie. It's not always appropriate to use, but here it lets us remove all your "or die..." statements and get down to the main code.

Removed the "$copylog" file, and just sent everything through STDERR using the warn command. Also was unsure why you had the "cp" line below the copy, but it wasn't helping. ;o)

And finally, your code would copy the file multiple times if it had more than one matching line. So here we add last after the first match, to tell Perl to leave the while loop and move on.

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') );

You may find that this still doesn't work in your environment, but if so, hopefully the error is a bit easier to suss out.

P.S. Since this is Perl and there is always more than one way to do it, you might find other CPAN options such as Path::Iterator::Rule more to your liking.

use strict; use warnings; use autodie; use Path::Tiny; use Path::Iterator::Rule; my $source = q(Z:\NEW\STUFF); my $pattern = qr(hello.*world); my $destination = q(C:\Users\loops\Desktop\stuff); close *STDERR; open *STDERR, '>', 'copylog.csv'; my $rule = Path::Iterator::Rule->new; $rule->file->line_match($pattern); my $next = $rule->iter( $source ); while ( defined( my $file = $next->() ) ) { eval { path($file)->copy($destination) }; print STDERR $@ ? "$@" : "$file, $pattern\n"; }

The only thing that may look out of place there is the eval block. This captures any failure of the copy, to let us report the result on the next line.


In reply to Re: Cannot copy files from linux shared to windows by Loops
in thread [Solved] Cannot copy files from linux shared to windows by bbb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found