Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[Solved] Cannot copy files from linux shared to windows

by bbb (Novice)
on Nov 05, 2014 at 07:39 UTC ( [id://1106157]=perlquestion: print w/replies, xml ) Need Help??

bbb has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Cannot copy files from linux shared to windows
by Loops (Curate) on Nov 05, 2014 at 11:17 UTC

    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.

      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!

Re: Cannot copy files from linux shared to windows
by Discipulus (Canon) on Nov 05, 2014 at 09:00 UTC
    ...the script doesn't work.
    is not very descriptive: it dies at some of your dies? what error Perl reports?
    Reading your code as I understand it you tell Perl something like:
    "Perl please for every file in the glob expression if it is a plain file, open it, read it and FOR EVERY occurence of a specified word copy this file to destination with File::Copy AND with the system call". you can use last after the first copy was succesfull.

    More notably you are calling the system call inside single quotes ('') and not backticks (``) that you can type with ALT+96 combo.
    Pay attention also to the directory separator on windows (you can safely use / instead of \ ): see Glob portabilty issues

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thank you for pointing out on how I can improve my scripting.

      I had updated the code removing the "cp -r " line for it really should not be included in my query.

      I'll modify the script and will let you know the result.

      Thanks again!

Re: Cannot copy files from linux shared to windows
by Anonymous Monk on Nov 05, 2014 at 08:36 UTC
    What is the error message?

      It says Copy failed: at copyfile.pl line 24

        It says Copy failed: at copyfile.pl line 24

        That's not good enough :)

        What is line 24?

        Is it the call to File::Copy::copy()

        or to  'cp -r ($File::Find::name, $target)'

        backticks do not communicate errors via $!

        You might try dying via sub Fudge for File::Copy::copy and Capture::Tiny for "cp.exe"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found