Here is another option, note that it does not "copy" empty directories:
use warnings; use strict; use File::Find; use File::Copy; my $from = 'C:/gash'; die "$from is not a directory" if not -d $from; my $to = 'C:/copy'; mkdir $to if not -e $to; find(\&wanted, 'C:/gash'); sub wanted { #$File::Find::dir is the current directory name, #$_ is the current filename within that directory #$File::Find::name is the complete pathname to the file. return if -d $File::Find::name; $File::Find::dir =~ /^$from(.*)/; my $copy_name; if (defined $1) { my $path = $to; # Create each directory in the path for my $dir (split /[\/\\]/, $1) { if ($dir) { # first element may be empty $path .= "/$dir" if $dir; mkdir $path if not -e $path; } } $copy_name = "$path/$_"; } else { $copy_name = "$to/$_"; } # Code added here my @stat = stat($copy_name); if (@stat && ! -w _) { my $perm = $stat[2] & 07777; chmod($perm | 0600, $copy_name); } # End of update print "$copy_name ($_)\n"; copy($File::Find::name, $copy_name) or die "Unable to copy $File::Find::name to $copy_name: $!"; }
This is a "quick and dirty" solution. Some improvements could be made, for example the return value for mkdir should be checked.

Update: OOPS! my solution did not overwrite read-only files. Code changed (# Code added here).

In reply to Re: How to recursively copy files in a folder to another folder? by cdarke
in thread How to recursively copy files in a folder to another folder? by Anonymous Monk

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.