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

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
  • Comment on Re: How can I compare two directories and copy over files missing from one to another?
  • Select or Download Code