Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy all the files of a particular type using Perl?

Tags:

file

copy

perl

Right now, I am using something like

copy catfile($PATH1, "instructions.txt"), catfile($ENV{'DIRWORK'});

individually for each of the .txt files that I want to copy. This code is portable as it does not use any OS specific commands.

How can I copy all the text files from $PATH1 to DIRWORK, when I do not know the individual names of all the files, while keeping the code portable?

like image 323
Lazer Avatar asked Jan 22 '23 05:01

Lazer


1 Answers

You can use the core File::Copy module like this:

use File::Copy;
my @files = glob("$PATH1/*.txt");

for my $file (@files) {
    copy($file, $ENV{DIRWORK}) or die "Copy failed: $!";
}
like image 83
Eugene Yarmash Avatar answered Feb 16 '23 02:02

Eugene Yarmash