I'm not sure if you wanting to copy or move a file. You have stated both - sometimes within the same post. For example, in Re^4: Check if file exists in multiple directories you initially say that you want to copy the file and then two sentences later you say that you want to move the file. And there is a significant difference between copying a file and moving a file.
In either case, I'm seeing one potential issue. For example, if there are 4 directories with a *.txt file and you copy/move 'file.out' from each one to the same new directory, you'll be overwriting the file in the new directory 3 times.
I kind of agree with most of 2teez's suggestions. Based on my understanding of what you're trying to do, here's my approach using File::Find::Rule and Path::Tiny. (NOTE: Both modules are not core modules and would need to be installed from CPAN. Also, I did not test the code below, so there's a chance there could be mistakes in it that I missed.)
use strict;
use warnings;
use File::Find::Rule;
use Path::Tiny;
my @dirs; # populate with list of directories to search
my $rule = File::Find::Rule->new;
$rule->file; # find only files
$rule->name(qr/\.txt$/i); # find (case-insensitive) filenames ending w
+ith .txt
$rule->extras({no_chdir => 1}); # don't change directories during sear
+ch
my @files = $rule->in(@dirs);
foreach my $file (@files) {
my $source_dir = path($file)->parent->stringify;
my $target_dir; # set this to path of target directory
my $outfile; # set to name of file to copy/move out of source dire
+ctory
my $source_file = path($source_dir,$outfile);
my $target_file = path($target_dir,$outfile);
# to copy the file, uncomment next line
#path($source_file)->copy($target_file);
# to move the file, uncomment next line
#path($source_file)->move($target_file);
}