in reply to Copying a directory recursively

With a bit of help from the File:: modules I've come up with this code snippet
use strict; use warnings; use File::Basename 'basename'; use File::Find::Rule 'find'; use File::Path 'mkpath'; use File::Copy 'copy'; use File::Spec::Functions qw/ splitdir catdir splitpath catfile /; $::PROG = basename $0; my($orig, $new) = @ARGV; die "Usage: $::PROG SRC DEST\n" unless defined $new and defined $orig; my $iter = find start => $orig; while(my $ent = $iter->match) { if(-d $ent) { my $mode = ( stat $ent )[2]; my @src = splitdir $ent; my $dest = catdir $new => @src; mkpath $dest => 0, $mode or die "$::PROG: Couldn't create '$ent': $!"; } else { my @src = splitpath $ent; my $dest = catfile $new => @src; copy $ent => $dest or die "$::PROG: Couldn't copy '$ent': $!"; } }
Not having a plethora of systems to work with, I couldn't say how portable that is, but given the ubiquity of File::Spec it should be relatively reliable. However, I'd say in this case it'd probably be simpler just to have a switch-esque statement to call the appropriate system copying tool per system to get reliable and consistent recursive copying.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Copying a directory recursively
by crabbdean (Pilgrim) on Mar 10, 2004 at 03:17 UTC
    Be wary of the File::Find module, it has a memory leak. See File::Find memory leak I tend to avoid it now.

    Is there a Win32 module similar to File::Dirsync or File::Repl mentioned above? My search on ppm3 didn't find anything. I'm looking for a similar solution myself. It my searches prove unsuccessful I'll end up writing my own, just researching now.

    Thanks
    Dean

    Programming these days takes more than a lone avenger with a compiler. - sam
    A Standard for the Transmission of IP Datagrams on Avian Carriers
      File::Repl works on Win32. ppm probably doesn't have it, but IIRC you can install from CPAN without needing a compiler.

      perl -MCPAN -e "install File::Repl".

      I would try that -- if you have problems, you may need to download a free copy of nmake from Microsoft (and possibly rename it make) to get CPAN to work, but other than that, it should be fine. Again, I don't think a compiler is required. We use this module at Work on Win32 quite often in our build system. No problems yet!

        The commands seemed to work, although its not completing the install. I also had to punch a few holes in my firewall. haha! Not knowing a lot about this way of compiling I'd probably need to research more. I'll have to look into it later or consult the group in another thread. Below is the trail end of the install to show the error. If you wish to respond write me at crabbdean at hotmail.com rather than dirty this thread with an adjunct conversation.

        Removing previously used \.cpan\build\Win32-API-0.41 CPAN.pm: Going to build A/AC/ACALPINI/Win32-API-0.41.tar.gz Checking if your kit is complete... Looks good Writing Makefile for Win32::API::Callback Writing Makefile for Win32::API -- OK Running make test test -- NOT OK Running make install make test had returned bad status, won't install without force Running make for D/DR/DROBERTS/File-Repl-1.20.tar.gz Is already unwrapped into directory \.cpan\build\File-Repl-1.20 CPAN.pm: Going to build D/DR/DROBERTS/File-Repl-1.20.tar.gz -- OK Running make test test -- NOT OK Running make install make test had returned bad status, won't install without force
        Cheers
        Dean

        Programming these days takes more than a lone avenger with a compiler. - sam
        A Standard for the Transmission of IP Datagrams on Avian Carriers