Jake
Jake

Reputation: 159

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!

Upvotes: 1

Views: 2645

Answers (2)

Hugmeir
Hugmeir

Reputation: 1259

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

Upvotes: 3

GreenAsJade
GreenAsJade

Reputation: 14685

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

Upvotes: 2

Related Questions