in reply to Recursive mkdir: is there already a module for this?
An implementation of createStructure:
use strict; use warnings; use File::Path; my %structure = ( dirs => { albemarle => { dirs => { chatauqua => {}, cattaraugus => {}, }, files => [ qw( washington adams jefferson ) ], }, beverly => { dirs => { ataturk => {}, bonaparte => {}, }, files => [ qw( madison monroe jackson ) ], }, cortelyou => { dirs => { peoria => {}, paducah => {}, }, files => [ qw( vanburen harrison tyler ) ], }, }, files => [ qw( alpha beta gamma ) ], ); createStructure ('./delme', \%structure); sub createStructure { my ($root, $hash) = @_; mkdir ($root) if ! -e $root; return if ! defined $root || ! keys %$hash; if (exists $hash->{dirs}) { for my $dir (keys %{$hash->{dirs}}) { createStructure ("$root/$dir", $hash->{dirs}{$dir}); } } if (exists $hash->{files}) { for my $file (@{$hash->{files}}) { next if -e "$root/$file"; open NEWFILE, '>', "$root/$file"; close NEWFILE; } } }
|
|---|