简体   繁体   中英

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. 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!

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]";
}

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.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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