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

发布于 2024-10-09 13:08:03 字数 113 浏览 0 评论 0原文

我有一个 txt 文件中的文件名列表。它们与我拥有的 pdf 相关。我想将该列表中列出的实际 pdf 文件复制到目录中。需要复制的文件也包含在不同的子目录中。使用 perl 执行此操作的最简单方法是什么? 谢谢!

I have a list of file names in a txt file. They correlate to pdfs I have. I would like to copy the actual pdf files that are listed in that list, to a directory. The files that need to be copied are contained in different subdirectories as well. What is the easiest way to do this using perl?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-10-16 13:08:03

我建议您阅读 perlfaq5: How do我复制文件

之后,应该不会太难;未经测试的代码:

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'd recommend that you read perlfaq5: How do I copy a file?

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]";
}
浴红衣 2024-10-16 13:08:03

我建议您使用 perl File::Copy 包。

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

I would recommend that you use the perl File::Copy package.

use File::Copy;
$filetobecopied = "myhtml.html.";
$newfile = "html/myhtml.html.";
copy($filetobecopied, $newfile) or die "File cannot be copied.";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文