Perl将文件复制到远程主机(Windows到Windows)

时间:2014-02-21 13:37:55

标签: perl

无法从localmachine

将文件复制到远程位置
use strict;
use warnings;
use File::Path qw(make_path);
use File::Copy

foreach my $All_Anr_File (@all_file)
{
    unless ($seen{$All_Anr_File}) 
    {
       # if we get here, we have not seen it before
        push(@UniqueAnr, $All_Anr_File);
        $seen{$All_Anr_File}++;

    }
}


foreach my $Anr_File (@UniqueAnr)
{
    (my $dir = $Anr_File) =~ s/\.[^.]+$//;
    my $new_dir= make_path("\\\\star\\\\8x26\\test\\$dir");  # remote path
    copy($Anr_File, $new_dir) or die "Copy failed for file $Anr_File: $!";
}

1 个答案:

答案 0 :(得分:2)

在标量上下文中,File::Path中的make_path函数不会返回刚刚创建的路径 - 它会返回创建的路径中步骤的数量。< / p>

star中的 "\\\\star\\\\8x26\\test\\$dir"后面还有一个多余的双反斜杠。我不确定这是否会影响结果。使用单引号更整洁,更可靠,因此大多数反斜杠不需要转义,File::Spec构建本机文件路径。

最佳做法是对局部变量名使用小写和下划线。大写字母保留给包名称等全局变量。

您的代码看起来应该是这样的。

use strict;
use warnings;

use File::Path qw(make_path);
use File::Copy qw(copy);
use File::Spec;

my @all_file;
my %seen;

# populate @all_file

my @unique_anr = grep !$seen{$_}++, @all_file;

for my $anr_file (@unique_anr) {
  (my $dir = $anr_file) =~ s/\.[^.]+$//;
  my $new_dir = File::Spec->catdir('\\\\star\8x26\test', $dir);
  make_path($new_dir);
  copy($anr_file, $new_dir) or die "Copy failed for file $anr_file: $!";
}