Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl dircopy和dirmove未按预期工作_Perl_Filesystems_File Handling - Fatal编程技术网

Perl dircopy和dirmove未按预期工作

Perl dircopy和dirmove未按预期工作,perl,filesystems,file-handling,Perl,Filesystems,File Handling,我正在编写一个Perl脚本,它允许我们轻松地将一个巨大的目录(可能+100000个子目录)移动或复制到另一个位置。为此,我使用了File::Copy::Recursive,如下所示(不完整!一些变量没有定义,但它给出了最基本的情况): 第一次运行时,这似乎与预期一样有效。接受这个命令 perl myscript.pl move source/ /home/otherdir/target/ 终端输出为 source/. did not match regex source/.. did not

我正在编写一个Perl脚本,它允许我们轻松地将一个巨大的目录(可能+100000个子目录)移动或复制到另一个位置。为此,我使用了
File::Copy::Recursive
,如下所示(不完整!一些变量没有定义,但它给出了最基本的情况):

第一次运行时,这似乎与预期一样有效。接受这个命令

perl myscript.pl move source/ /home/otherdir/target/
终端输出为

source/. did not match regex
source/.. did not match regex
就这样

但是,当我在移动的文件夹上运行相同的脚本时,会出现问题

perl myscript.pl move /home/otherdir/target/ /home/failing/target/

/home/otherdir/target/. did not match regex
/home/otherdir/target/.. did not match regex
/home/otherdir/target/somefile1.txt not a dir
/home/otherdir/target/somefile2.txt not a dir
/home/otherdir/target/somefile3.txt a dir
/home/otherdir/target/somefile4.txt a dir
显然,在数据上运行相同的复制/移动脚本时,我不应该得到不同的响应。然而,奇怪的是,这些文件来自一个目录(我无法确定它们在内容方面都是相同的目录),而其他目录则被保留下来。因此,在每次运行中,对于一个$sub_dir,脚本将目录的内容复制到目标,而不是目录本身。这意味着每次运行脚本时都会丢失一个目录。。。但我不明白为什么

我认为我误用了
dircopy
dirmove
,我不确定
$File::Copy::Recursive::CPRFComp=1
也是正确的(对于新手来说,我没有发现文档非常清晰)。有什么想法吗


在进一步挖掘之后,我认为这就是发生的情况。CPRFComp上的小文档读取(假设为“foo/file”):


因此,我的猜测是,subdir的第一个复制/移动操作是在目标位置(“示例中的bar”)存在之前触发的,这将导致bar/file而不是bar/foo/file。然后问题变为:如何确保复制/移动操作等待目标目录生成?

为了确保在移动或复制任何子目录之前存在路径,我只需使用File::path模块的
make_path
在执行操作之前创建路径。简单地说,就像这样:

if ($action eq 'copy') {
  make_path($target_location);
  dircopy("$source_location/$sub_dir/", "$target_location/")
        or die "Cannot copy $source_location/$sub_dir/: $!\n";
} elsif ($action eq 'move') {
  make_path($target_location);
  dirmove("$source_location/$sub_dir/", "$target_location/")
        or die "Cannot move $source_location/$sub_dir/: $!\n";
}

您的代码显示您正在打印
$source\u location/$sub\u dir与regex不匹配,但您的错误消息显示
。与正则表达式不匹配,缺少斜杠。因此,您要么没有显示真正的代码,要么没有显示真正的输出。请显示您的实际代码和实际错误消息,否则我们无法知道我们发现的问题是您的实际问题,还是您的问题发布错误的结果。@Paul我在一个示例中编辑过。我不太明白为什么这会被否决。如果我需要提供更多信息,请告诉我。
dircopy('foo', 'bar') or die $!;
# if bar does not exist the result is bar/file
# if bar does exist the result is bar/file

$File::Copy::Recursive::CPRFComp = 1;
dircopy('foo', 'bar') or die $!;
# if bar does not exist the result is bar/file
# if bar does exist the result is bar/foo/file
if ($action eq 'copy') {
  make_path($target_location);
  dircopy("$source_location/$sub_dir/", "$target_location/")
        or die "Cannot copy $source_location/$sub_dir/: $!\n";
} elsif ($action eq 'move') {
  make_path($target_location);
  dirmove("$source_location/$sub_dir/", "$target_location/")
        or die "Cannot move $source_location/$sub_dir/: $!\n";
}