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); }

In reply to Re: Check if file exists in multiple directories by dasgar
in thread Check if file exists in multiple directories by Bama_Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.