Hi Monks ! I am a beginner looking for some help.
I have 2 directories, Source and Target and I would like to take every directory/file in Source dir that is not in Target dir and copy it over to Target dir. This should include sub dirs as well.
If dir/file from Source is already in Target, I would like to print its dir/file name.
So far I came up with the follwoing code, but it doesnt seem to be working.
Usage: perl mergeTrees.pl SourceDir TargetDir
use strict; use File::Find; use File::Copy; use Cwd; my $Src = $ARGV[0]; my $Trg = $ARGV[1]; my $path = getcwd(); find (\&wanted, $Src); sub wanted { opendir (DIR, "$path/$Trg") or die "cannot opendir $Trg"; foreach my $file (readdir(DIR)) { if ($_ == $file) { print "File already in $Trg: $file\n"; } else { copy("$_","$path/$Trg/$_"); } } closedir (DIR); }

In reply to How can I compare two directories and copy over files missing from one to another? by habhab

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.