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: $!"; }