in reply to cloning a directory tree

If you think of a "directory tree" as a real tree gaph, the minimum number of mkpath() calls you need is exactly the number of leaves in that tree. find() does a depth-first search, so there's a quite efficient way to determine leaves:

my $p = './a'; my @paths = (); find( sub { return unless -d; print "$File::Find::name in $File::Find::dir\n"; unless( $p eq $File::Find::dir ){ print "CH\n"; push @paths => $p; } $p = $File::Find::name; } , $p ); push @paths , $p; print "--> $_\n" for @paths; # this has been tested

The amount of saved mkpath() calls depends on the directory tree, it's still O(n) where n is the number of directories but that's only reached if the tree is super-flat (depth=1).

But why use mkpath at all? It ends up in n mkdir() calls anyways, why not do that yourself in the find-sub and save the time mkpath() needs to recognize already created directories? mkpath() is not usefull if you do directory traversal anyways. For find() uses depth-first search, it's guaranteed that the parent directory always exists.

find( sub { next unless -d; mkdir File::Spec->catpath( ... ) } , ' ... ' );
--
http://fruiture.de