NT: perl 5.8.0: system call - commnad split into multiple line ?

E

Eric

Greetings,

I am fairly new to Perl. I downloaded Perl 5.8.0 for NT/Win2000
platform.

I want to copy file from server1 to server2 using robocopy. In my perl
script:

$datenow = `date /T`;
($day,$mth,$yr) = split /\//, $datenow;

$src = "\\\\server1\\d1\\$yr\\$mth";
$dest = "\\\\server2\\d2\$yr\\$mth";
$swth = "\/r:1 \/w:5 \/MIR";

$comm = "robocopy $src\\$yr\\$mth $dest\\$yr\\$nth $swth";

print "system($comm)";

When I checks the system($comm) construct using print, it shows the
$comm string in multiple lines. For example:

system(robocopy \\server1\d1\2003
\09 \\server2\d2\2003
\09 /r:1 /w:5 /MIR)

Is there any way to get the $comm string into one line ? I believe
because it split into multiple line, robocopy gets the parameter wrong
and therefore won't work.

Did I miss something ? Can anyone help ?

Any pointer appreciated.

TIA

Eric
 
D

David K. Wall

Eric said:
Greetings,

I am fairly new to Perl. I downloaded Perl 5.8.0 for NT/Win2000
platform.

I want to copy file from server1 to server2 using robocopy. In my
perl script:

For your own safety and sanity, enable strict and warnings:

use strict;
use warnings;

Among other things, 'strict' will force you to declare your
variables, and warnings will tell you about all sorts of real and
potential errors. Unless you have a good reason otherwise, these two
statements should start all your programs.
$datenow = `date /T`;

$datenow has a newline on the end of it. (Actually CR-LF since this
is windows, but never mind.)
($day,$mth,$yr) = split /\//, $datenow;

Now $yr has a newline on the end of it.

Instead of calling an external program, it's usually easier to use
Perl's localtime() function.

my ($day, $mth, $yr) = (localtime)[3,4,5];
$yr += 1900;
$mth++;

perldoc -f localtime
$src = "\\\\server1\\d1\\$yr\\$mth";
$dest = "\\\\server2\\d2\$yr\\$mth";
$swth = "\/r:1 \/w:5 \/MIR";

There's no need to escape the forward-slashes here. You're not
inside a slash-delimited regex.
$comm = "robocopy $src\\$yr\\$mth $dest\\$yr\\$nth $swth";

If you had enabled strict or warnings (or both), perl would have
given you an error message when you tried to use $nth.
print "system($comm)";

When I checks the system($comm) construct using print, it shows
the $comm string in multiple lines. For example:

system(robocopy \\server1\d1\2003
\09 \\server2\d2\2003
\09 /r:1 /w:5 /MIR)

Yes, right after each place where you use $yr.
Is there any way to get the $comm string into one line ? I believe
because it split into multiple line, robocopy gets the parameter
wrong and therefore won't work.

No, it's because `date /t` returns a string with a newline on the
end.
 
E

Eric

Thankyou. I learn something today and I have a long way to go before I
get to know perl :)

Eric

David K. Wall said:
Eric said:
Greetings,

I am fairly new to Perl. I downloaded Perl 5.8.0 for NT/Win2000
platform.

I want to copy file from server1 to server2 using robocopy. In my
perl script:

For your own safety and sanity, enable strict and warnings:

use strict;
use warnings;

Among other things, 'strict' will force you to declare your
variables, and warnings will tell you about all sorts of real and
potential errors. Unless you have a good reason otherwise, these two
statements should start all your programs.
$datenow = `date /T`;

$datenow has a newline on the end of it. (Actually CR-LF since this
is windows, but never mind.)
($day,$mth,$yr) = split /\//, $datenow;

Now $yr has a newline on the end of it.

Instead of calling an external program, it's usually easier to use
Perl's localtime() function.

my ($day, $mth, $yr) = (localtime)[3,4,5];
$yr += 1900;
$mth++;

perldoc -f localtime
$src = "\\\\server1\\d1\\$yr\\$mth";
$dest = "\\\\server2\\d2\$yr\\$mth";
$swth = "\/r:1 \/w:5 \/MIR";

There's no need to escape the forward-slashes here. You're not
inside a slash-delimited regex.
$comm = "robocopy $src\\$yr\\$mth $dest\\$yr\\$nth $swth";

If you had enabled strict or warnings (or both), perl would have
given you an error message when you tried to use $nth.
print "system($comm)";

When I checks the system($comm) construct using print, it shows
the $comm string in multiple lines. For example:

system(robocopy \\server1\d1\2003
\09 \\server2\d2\2003
\09 /r:1 /w:5 /MIR)

Yes, right after each place where you use $yr.
Is there any way to get the $comm string into one line ? I believe
because it split into multiple line, robocopy gets the parameter
wrong and therefore won't work.

No, it's because `date /t` returns a string with a newline on the
end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top