[英]use list of files to to copy and move to new directory in perl
我在txt文件中有文件名列表。 它們與我擁有的pdf相關。 我想將列表中列出的實際pdf文件復制到目錄中。 需要復制的文件也包含在不同的子目錄中。 使用Perl最簡單的方法是什么? 謝謝!
我建議您閱讀perlfaq5: 如何復制文件 ?
之后,它應該不會太難。 未經測試的代碼:
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]";
}
我建議您使用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.