当前位置: 动力学知识库 > 问答 > 编程问答 >

syntax - problems with a perl "system" script

问题描述:

Im trying to write a very simple script that copies files from several remote computers to my computer. I've written the following in perl:

@Comps = ("comp1","comp2","comp3","comp4");

foreach $comp (@Comps)

{

system("copy /Y \\\\$comp\\c\$\\myfolder\\$comp*.log C:\\Program Files\\My Cont Folder\\input\\");

}

I get a syntax error and cant understand why.

Thanks!

网友答案:

Given the case that Perl is passing the characters to the shell, the only way that the shell will process "C:\Program Files" as one argument is to put double quotes around it. (Or sometimes just with one double quote preceding it.)

So you might need a few more backslashes (so I qq-ed it):

system( qq{cmd /c copy /Y \\\\$comp\\c\$\\myfolder\\$comp*.log "C:\\Program Files\\My Cont Folder\\input\\"} )
网友答案:

Looks like you've got error in the command itself, because the perl code ran fine. (Gave me 4 times "command not found" since I'm on Unix).

I would suggest using list form of system() call: system("copy", "/Y", $src, $dst); and, probably, even sprintf() to fill out $src and $dst.

Of course, you can always check what exactly gets executed by running print before system. I often hesitate and often print.

网友答案:

Perl and windows paths don't mix very well, why not use File::Copy instead?

use File::Copy;
copy($file1,$file2) or die "Copy failed: $!";

And then you can use forward slash in your paths instead. Much easier.

分享给朋友:
您可能感兴趣的文章:
随机阅读: