简体   繁体   English

使用文件列表复制并移动到Perl中的新目录

[英]use list of files to to copy and move to new directory in perl

I have a list of file names in a txt file. 我在txt文件中有文件名列表。 They correlate to pdfs I have. 它们与我拥有的pdf相关。 I would like to copy the actual pdf files that are listed in that list, to a directory. 我想将列表中列出的实际pdf文件复制到目录中。 The files that need to be copied are contained in different subdirectories as well. 需要复制的文件也包含在不同的子目录中。 What is the easiest way to do this using perl? 使用Perl最简单的方法是什么? Thanks! 谢谢!

I'd recommend that you read perlfaq5: How do I copy a file ? 我建议您阅读perlfaq5: 如何复制文件

After that, it shouldn't be too hard; 之后,它应该不会太难。 Untested code: 未经测试的代码:

use strict;
use warnings;
use 5.010;
use autodie;
use File::Copy;
use File::Spec;

open my $files_fh, '<', '/path/to/txt';

my $files_dir       = shift // '/path/to/dir';
my $destination_dir = shift // '/path/to/dir';

while (<$files_fh>) {
    chomp;
    next unless -d (my $file = File::Spec->catfile($files_dir, $_) );

    copy($file, $file . '.cpy');
    move($file . '.cpy', $destination_dir);
    say "Copied [$file] and moved it to [$destination_dir]";
}

I would recommend that you use the perl File::Copy package. 我建议您使用perl File :: Copy包。

use File::Copy;
$filetobecopied = "myhtml.html.";
$newfile = "html/myhtml.html.";
copy($filetobecopied, $newfile) or die "File cannot be copied.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM