Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Cannot copy files from linux shared to windows

by Loops (Curate)
on Nov 05, 2014 at 11:17 UTC ( [id://1106181]=note: print w/replies, xml ) Need Help??


in reply to [Solved] Cannot copy files from linux shared to windows

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.

Replies are listed 'Best First'.
Re^2: Cannot copy files from linux shared to windows
by bbb (Novice) on Nov 06, 2014 at 00:24 UTC

    Thank you for sharing your thoughts on this.

    Basically, I had said that the script is having an error or not working is because I cannot copy files when I point the source dir to the Linux shared drive. But during testing in my windows 7 local drives, it is working fine.

    Still, I will keep in mind the ideas you have shared.

    I will try to modify the script and I will inform you the result.

    Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1106181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found