Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to recursively copy files in a folder to another folder?

by Anonymous Monk
on Dec 10, 2009 at 10:38 UTC ( #812152=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I mean, how can I do an "XCopy"(win32 dos command) work using perl? and I can't use those components.
use File::Xcopy; use File::Copy::Recursive; # and other components that are NOT included in ActivePerl-5.8.0.805-M +SWin32-x86.msi
Because this function will be used in machines that only installed ActivePerl-5.8.0.805-MSWin32-x86.msi

Comment on How to recursively copy files in a folder to another folder?
Download Code
Re: How to recursively copy files in a folder to another folder?
by kiinoo (Novice) on Dec 10, 2009 at 10:41 UTC
    Monks, this question is posted by me, sorry for not logged in.
Re: How to recursively copy files in a folder to another folder?
by moritz (Cardinal) on Dec 10, 2009 at 10:42 UTC
Re: How to recursively copy files in a folder to another folder?
by kiinoo (Novice) on Dec 10, 2009 at 10:44 UTC
    All I want to do is copying everything in a folder to another and if the file does already existed (or readonly) in the destination folder, it will be overwritted.

      As moritz said, you can call XCOPY with system, but I'm not sure you will find an option to overwrite an existing readonly file in the destination folder.

Re: How to recursively copy files in a folder to another folder?
by cdarke (Prior) on Dec 10, 2009 at 14:51 UTC
    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).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812152]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-07-11 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls