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

I have 2 directory say of below format: sourcedir/<ENV>/<CLIENT> to targetdir/<ENV>/<CLIENT> I have to do this in perl. Can someone tell me a efficient way to do this. The ENV and CLIENT changes as they are like directories. How can i recursively go into them and make sure the files get copied.
  • Comment on Copying files from one directory to another with different subdir

Replies are listed 'Best First'.
Re: Copying files from one directory to another with different subdir
by Corion (Patriarch) on Sep 19, 2016 at 11:56 UTC

    What code do you already have and how does it fail to do what you want?

    Most likely, your code will be using File::Copy or File::Copy::Recursive.

    Also, have you considered simply using:

    system(qq{cp -rp '$source_dir' '$target_dir'}) == 0 or die "$! / $?";

    ... or on Windows

    system(qq{xcopy /v /q /i /e /s "$source_dir" "$target_dir"}) == 0 or die "$! / $?";
      But how do i get the subdir recursively.so that the file from dir A/B goes to C/B and so on also for F/B goes to G/B

        That's what the cp -r does. Did you try one of those commands and it did not work? What did happen?

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

        Maybe you can show some examples of what you have as source directory structure and target directory structure and how both source and target are specified. Then we can better advise you on the approach to take.