Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Replication of directory subtrees

by rbi (Monk)
on Apr 24, 2003 at 14:13 UTC ( #252905=perlquestion: print w/replies, xml ) Need Help??
rbi has asked for the wisdom of the Perl Monks concerning the following question:

I have some directory trees like these dir1 and dir2 under directory origin:
origin dir1 subdir1 subdir2 subdir3 file1 file2 dir2 subdir1 subdir2 subdir3 file1 file2
that I want to copy under directory destination. They contain both directories and files.
I came up with this where I can also set a parameter to specify the depth of replication, and it seems to work, but I wonder if there is a more elegant/shorter way to do this.
use strict; use File::Copy; use File::Basename; use File::Path; use File::Glob ':glob'; my $din = 'c:/temp/origin/'; my $dout = 'c:/temp/destination/'; docp($din,$dout,1); sub docp { my $din = shift(); # ORIGIN DIRECTORY my $dout = shift(); # DESTINATION DIRECTORY my $tree = shift(); # DEPTH OF DIRECTORY RECURSION my ($f,$d,$b); die if -e $dout; mkpath($dout); my @list = bsd_glob($din."*"); map { ($f,$d) = fileparse($_); if (-d and $tree) { $f = $f.'/'; $b = $_.'/'; docp($b,$dout.$f,$tree) if $tree ge 0; } else { copy ($_,$dout.$f); } } @list; $tree = $tree - 1; }
Thank you in advance.

Replies are listed 'Best First'.
•Re: Replication of directory subtrees
by merlyn (Sage) on Apr 24, 2003 at 14:30 UTC
Re: Replication of directory subtrees
by Abigail-II (Bishop) on Apr 24, 2003 at 14:29 UTC
    I'd do system cp => '-r', $din, $dout;, or if I'm on a system that doesn't have a recursive copy, I'd do: chdir $din or die; system "tar cf - '$din' | (cd $dout; tar xf -)".


      Or there's always rsync, too.

        Well, not always. POSIX compliant systems will have cp and tar, and many non POSIX compliant systems will too. (Anything remotely smelling like Unix, including Cygwin). rsync on the other hand isn't a standard tool.

        But yes, rsync will work, just like scp. Or you could make a business copy and split off a mirror. But that's not standard either.


Re: Replication of directory subtrees
by eduardo (Curate) on Apr 24, 2003 at 14:52 UTC

    I hate to offer a non-perlish solution, but have you ever considered using xcopy? It would seem like you are on a win32ish system... and xcopy is exactly what this was designed for.

    All that being said, if you need this to be cross platform, you are *way* better off with some of the other monks answers :)

      Yes, It is going to be subroutine within a CGI application that should run both under MsWin32 and Unix.
        well, in that case!
        if ($^O =~ /win32/i) { # do stuff with xcopy } else { # do stuff with cp -r }
        I am *not* advocating this solution... I just thought it would be cheesy way to do it. Please, for the love of anything you may or may not hold holy, use something like File::Find or File::NCopy instead like merlyn suggested.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://252905]
Approved by smitz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2017-07-26 15:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (398 votes). Check out past polls.