Mjpaddy has asked for the wisdom of the Perl Monks concerning the following question:

hello monks, i have 2 dir test1 and test2 and test1 dir contain 2 files abc.txt and xyz.txt while in test2 dir contain 3 files abc.txt, sdf.txt and fgh.txt. i just want two copy the files in to another dir like xyz.txt is not in test2 so copy it and placed it in test2 likewise make both dir same not touch the abc.txt because it present in both although their contains are differert.

Replies are listed 'Best First'.
Re: dir handle
by choroba (Cardinal) on May 27, 2014 at 08:19 UTC
    Recently, I've been trying to use Path::Tiny to solve similar problems, as it has everything you need in one place.
    #!/usr/bin/perl use warnings; use strict; use Path::Tiny; setup(); # This is the solution: for my $file (path('test1')->children) { $file->copy('test2') unless path('test2/' . $file->basename)->exis +ts; } sub setup { my %structure = ( test1 => [qw( abc.txt xyz.txt )], test2 => [qw( abc.txt sdf.txt fgh.txt )], ); my $i = 0; for my $dir (keys %structure) { for my $file (@{ $structure{$dir} }) { path("$dir/$file")->touchpath->spew($i++); } } }

    Update: you might need to run the loop twice (with a small modification) to copy files from test2 to test1 if it's desired as well.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      can you do it without oop!
        Sure I can, but why?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: dir handle
by taint (Chaplain) on May 27, 2014 at 08:08 UTC
    Greetings, Mjpaddy.

    Before responding. I'd just like to make sure I'm following you correctly.
    It sounds like you have 2 directories, each with different files. Except the file "abc.txt", which is located in both directories. Is that correct?

    Best wishes.

    --Chris

    ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      your guess is right so i want that make both dir similar as per the file. but abc.txt content in both dir is different. can you do it without opp.
Re: dir handle
by Anonymous Monk on May 27, 2014 at 08:08 UTC