CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to copy files to windows share using perl

 



Tejas
User

Jan 9, 2015, 10:16 AM

Post #1 of 25 (7648 views)
How to copy files to windows share using perl Can't Post

Hi
Is there a way to copy files to windows share using perl

Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 9, 2015, 10:25 AM

Post #2 of 25 (7647 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

Use the system() function to execute the Windows copy command and specify a UNC path in the command.

http://msdn.microsoft.com/en-us/library/gg465305.aspx


Tejas
User

Jan 9, 2015, 10:27 AM

Post #3 of 25 (7645 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

Will try and get back :)

Thanks
Tejas


Tejas
User

Jan 19, 2015, 10:39 PM

Post #4 of 25 (7484 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

 
Hi Fishmonger

the below code doesnt work

my $file_location = "./test_data.xlsx"
my $share = '\\ant\func\Comparison\share.xlsx';

eval {
copy($file_location, $share) || die("Copy failed: $!\n");
print("file copied\n");
};

The file isnt getting copied to windows share, but a new file is getting created in present working directoy in my linux box

Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 20, 2015, 6:11 AM

Post #5 of 25 (7480 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

Print out $share to verify it is the path you expect.

If it is the correct path, then what happens when you try to do a dir listing on it outside of the script? Do you get prompted to enter a username/password?


(This post was edited by FishMonger on Jan 20, 2015, 6:14 AM)


Tejas
User

Jan 20, 2015, 6:27 AM

Post #6 of 25 (7474 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

ok


Its actually creating a file with this path name

Nope,it is nt asking for password.
Its just creating a new file in pwd

Will check it again
And , how to get started with Object Oriented perl.
From Fundamentals .
I think its time for me to start OO Perl.

Thanks


FishMonger
Veteran / Moderator

Jan 20, 2015, 7:26 AM

Post #7 of 25 (7472 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

You obviously didn't print out the value of $share like I asked. If you had you would have discovered the problem.

The backslash is the escape character. If you need a literal backslash, you need to use a second one to escape it's special meaning.

Code
my $share = '\\\\ant\\func\\Comparison\\share.xlsx';


Personally, I prefer to use forward slashes for the path separator, which is what the OS uses internally.

Code
my $share = '//ant/func/Comparison/share.xlsx';


Test code:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy qw(copy);

my $share = '//iso/data/test/fishmonger';
my $file = './test.txt';

copy($file, $share) or die $!;


Test results:

Code
c:\test>dir /B \\iso\data\test\fishmonger 

c:\test>copy2share.pl

c:\test>dir /B \\iso\data\test\fishmonger
test.txt



(This post was edited by FishMonger on Jan 20, 2015, 7:29 AM)


Tejas
User

Jan 20, 2015, 7:58 AM

Post #8 of 25 (7467 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

I dint print.
The thing is that the path i have is nt original path.
The path that i have printed is alias name of a host

Are you copying the file from linux to windows share?

This question is offtopic

Quote
i need to print $O8 to a excel sheet
is the below code correct
my $row = 8 ;
my ($init_row,$fin_row) = (1,2);
my $formula =
{
0 => sub { my $row = shift; $row = $row ; qq/=SUM(\$O\$${init_row}:\$O\$${fin_row}) > 0},

}

$formula->{0}->($init_row,$fin_row) ;

will the abpove code work for acheiving the output below
OUTPUT
SUM($O$1,$0$2) > 0









Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 20, 2015, 8:12 AM

Post #9 of 25 (7465 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post


In Reply To
I dint print.
The thing is that the path i have is nt original path.
The path that i have printed is alias name of a host

Are you copying the file from linux to windows share?

I copied the file from Windows to a Linux samba share.



In Reply To
This question is offtopic

Quote
i need to print $O8 to a excel sheet
is the below code correct
my $row = 8 ;
my ($init_row,$fin_row) = (1,2);
my $formula =
{
0 => sub { my $row = shift; $row = $row ; qq/=SUM(\$O\$${init_row}:\$O\$${fin_row}) > 0},

}

$formula->{0}->($init_row,$fin_row) ;

will the abpove code work for acheiving the output below
OUTPUT
SUM($O$1,$0$2) > 0


What happened when you tried?


Tejas
User

Jan 20, 2015, 9:39 AM

Post #10 of 25 (7455 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post


Quote
I copied the file from Windows to a Linux samba share.

So , Can i copy files from my linux machine to Windows DFS , Windows Share


Quote
my ($init_row,$fin_row) = (1,2);
my $formula =
{
0 => sub { my ($init_row,$fin_row) = @_;qq/=SUM(\$O\$${init_row}:\$O\$${fin_row}) > 0/},
};

print $formula->{0}->($init_row,$fin_row) ;


Output

=SUM($O$1:$O$2) > 0

The out put is perfect .
But Can u explain me about qq , quotes and backlashes


FishMonger
Veteran / Moderator

Jan 20, 2015, 10:05 AM

Post #11 of 25 (7454 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post


Quote
So , Can i copy files from my linux machine to Windows DFS , Windows Share

Yes, as long as the share is setup correctly.


Quote
But Can u explain me about qq , quotes and backlashes

http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators


Tejas
User

Jan 20, 2015, 10:25 AM

Post #12 of 25 (7447 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

my $formula =
{
0 => sub { my ($init_row,$fin_row) = @_;qq/=SUM(\$O\$${init_row}:\$O\$${fin_row}) > 0/},
};
What exactly is the value and why is it returning the formula in quotes
Why is mr it returning everything in func

Can u explain what this hash ref does
1. It has a key 0 with a value which is a function
But I'm not returning anything
How is the formula returned

Could you please throw some light on it
Need insight for understanding it better

Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 20, 2015, 12:18 PM

Post #13 of 25 (7445 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

Based on your questions it's obvious that someone else wrote that code for you. Can you post a link to where you got it.

The hash ref is a dispatch table.
Implementing Dispatch Tables => http://www.perlmonks.org/?node_id=456530


Laurent_R
Veteran / Moderator

Jan 20, 2015, 2:55 PM

Post #14 of 25 (7441 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

Hi Tejas,

given your beginner level in Perl and probably in software programming in general (I am saying that with all due respect, we have all started one day as beginners), and even though you have made good progress lately, I would not recommend that you start using dispatch tables. This requires understanding of code references, and this is definitely not a beginner-level concept, but rather at least an intermediate (or possibly even advanced level) concept.

Having said that, if you really want to take that route, I'll be happy to help.


Tejas
User

Jan 20, 2015, 6:45 PM

Post #15 of 25 (7438 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

Hi
Well, this code is recommended by zhris
And I understand what code references are based on your explaination in my earlier posts here
There is no link where I found this as it has been recommended here
Code ref actually stores the address of the func and when ever the hash is called , it branch s to the address of that func and then executes the func
My doubt was a rather simple one, we do it have a return statement and it is just printing the formula
Dispatch tables are almost used in every code I ve written after ur recommendation.

Thanks
Tejas


Tejas
User

Jan 20, 2015, 7:18 PM

Post #16 of 25 (7431 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

here is the link
http://perlguru.com/gforum.cgi?post=80552;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread

And unknowingly i ve been working on dispatch tables , even before i learnt basics in perl.
Even that was recommeneded in PerlGuru, but dint undertand the concept properly then , as it was effective i used it everywhere and wherever it is necessary.

Thanks
Tejas


Laurent_R
Veteran / Moderator

Jan 20, 2015, 11:14 PM

Post #17 of 25 (7427 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post


In Reply To
Dispatch tables are almost used in every code I ve written ...


Alright, forget what I said in my earlier post, then.


Tejas
User

Jan 20, 2015, 11:17 PM

Post #18 of 25 (7426 views)
Re: [Laurent_R] How to copy files to windows share using perl [In reply to] Can't Post

Biggest joke is that , i have been using it unknowingly.
But understood it from the link i ve posted , which has ur explaination.

Just had a sily doubt that there is no return statement, and how is it returning ?

Imagine Something like
my ($init_row,$fin_row) = (1,2);
my $formula =
{
'set' => sub { my ($init_row,$fin_row) = @_;
return qq/=SUM(\$O\$${init_row}:\$O\$${fin_row}) > 0/},
};

print $formula->{'set'}->($init_row,$fin_row) ;


Code
 
Dont know whether this works , but we can clearly see that func or the coderef returning .
In Dispatch , what is returned and whsta not returned is my excast doubt
ThANKS
tEJAS


FishMonger
Veteran / Moderator

Jan 21, 2015, 6:13 AM

Post #19 of 25 (7424 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

Your doubt about what is returned by the sub is a perfect example why reading the related documentation is very important.


Code
perldoc perlsub

4th & 5th paragraphs:

Code
    A "return" statement may be used to exit a subroutine, optionally 
specifying the returned value, which will be evaluated in the
appropriate context (list, scalar, or void) depending on the context of
the subroutine call. If you specify no return value, the subroutine
returns an empty list in list context, the undefined value in scalar
context, or nothing in void context. If you return one or more
aggregates (arrays and hashes), these will be flattened together into
one large indistinguishable list.

If no "return" is found and if the last statement is an expression, its
value is returned.
If the last statement is a loop control structure
like a "foreach" or a "while", the returned value is unspecified. The
empty sub returns the empty list.



(This post was edited by FishMonger on Jan 21, 2015, 6:13 AM)


Tejas
User

Jan 21, 2015, 8:32 AM

Post #20 of 25 (7420 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

This excatly clears my doubt ,
i should have done this my self , but i excatly did not know what tp search for, i kept reading about dispatch tables , but not perlsub.

And its different from what C++ does actually.
I work on c++ mostly and it doesnt return anything if not specified.

These are exciting features in perl.


More over how would i know that i have to seatrch for perlsub, is it intutive ?

Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 21, 2015, 9:00 AM

Post #21 of 25 (7418 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

You should familiarize yourself with ALL of the core documentation.

c:\>perldoc perl

http://perldoc.perl.org/


Tejas
User

Jan 21, 2015, 9:02 AM

Post #22 of 25 (7416 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

Thanks You FishMonger


Tejas
User

Jan 26, 2015, 11:04 PM

Post #23 of 25 (7370 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post

hi


Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy qw(copy);

#my $share = '//iso/data/test/fishmonger';
my $share = '\\\\FILE-FIN-9002\\Accounting\\test.txt';
my $file = './test.txt';
print "Name: $share";
copy($file, $share) or die $!;


It is again creating a directory in my pwd but not copying to windows share

output
Name: \\FILE-FIN-9002\Payments-Accounting\test.txt


Why is this code not considering network at all.
it is just copying a file to other file in same foldER

Thanks
Tejas

Quote
and when i change share name to $share = '\\172.27.42.28\Payments-Accounting\test.txt '
it is still creating the file with this name

I repeat, the share is a nfs share



(This post was edited by Tejas on Jan 26, 2015, 11:34 PM)


Tejas
User

Jan 26, 2015, 11:22 PM

Post #24 of 25 (7367 views)
Re: [FishMonger] How to copy files to windows share using perl [In reply to] Can't Post


Quote
Print out $share to verify it is the path you expect.

If it is the correct path, then what happens when you try to do a dir listing on it outside of the script? Do you get prompted to enter a username/password?


It is printing the share name just as it prints a file name
//FILE-FIN-9002/Accounting/test.txt

and it is creating a file with this name

and how to do dir listing outside the script

is it just
ll //FILE-FIN-9002/Accounting
Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 27, 2015, 6:59 AM

Post #25 of 25 (7358 views)
Re: [Tejas] How to copy files to windows share using perl [In reply to] Can't Post

It's been a long time since I last setup this type of share and I had forgotten a key step in the process.

After setting up the share in Windows, which is all that's needed to share between Windows systems, in order for a Linux system to access that share, you also need to create a mount point on the Linux system. The mount point can be thought of as being comparable to a Windows drive mapping.

http://www.cyberciti.biz/faq/access-windows-shares-from-linux/
http://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives