My first piece of advise is that there is a much easier way to test if a file exists in a directory than reading its contents and iterating through each one doing a comparison. To see if a file exists try:

if(-f $path_to_file){ #do something }

The second problem I see is that $_ will be set to the name of a file, so if the file is in a subdirectory (and you said you wanted to include subdirs) testing -f $path.'/'.$Trg.'/'.$_ would not work. Try this:

use strict; use File::Find; use File::Copy; use Cwd; my $Src = $ARGV[0]; my $Trg = $ARGV[1]; my $path = getcwd(); chdir($Src); find (\&wanted, '.'); sub wanted { if(-f $path.'/'.$Trg.'/'.$File::Find::dir.'/'.$_){ print "File in $Trg: $_\n"; }else{ mkdir($path.'/'.$Trg.'/'.$File::Find::dir) if(! -d $path.'/'.$Tr +g.'/'.$File::Find::dir); copy($_, $path.'/'.$Trg.'/'.$File::Find::dir.'/'.$_) if(! -d +$File::Find::name); } }
May the Force be with you

In reply to Re: How can I compare two directories and copy over files missing from one to another? by JediWizard
in thread 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.