Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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:

Hi,
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.
roberto

Comment on Replication of directory subtrees
Select or Download Code
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 -)".

    Abigail

      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.

        Abigail

•Re: Replication of directory subtrees
by merlyn (Sage) on Apr 24, 2003 at 14:30 UTC
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.
      Thanks.
        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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-08-23 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls