There is the File::Path module (in the standard distribution of Perl even), which does exactly that, create intermediate directories if they don't exist.
use Path::Class qw(dir);
dir("foo", "bar")->mkpath;
system( "mkdir -p $dirname" );
File::Flat->write( '/some/path/that/might/exist.txt', $contents );Of course, the neatest thing is that this...
File::Flat->canWrite( '/some/path/that/might/exist.txt' );... also "means what you think", that is, "Can I write to the file, or create the file, or create as many directories as are needed".
$path =~ s[/][\\]g; system "md $path";
Here is somthing I wrote to do what you need.
#Note this is windows centric
use strict;
make_dir("c\:\\this\\is\\a\\deep\\subdirectory\\");
sub make_dir {
my ($inpath) = @_; #Full directory path
my $path = ""; #Storage path
#Loop through the sections
foreach(split/\\/,$inpath) {
#Add to path
$path .= $_ . "\\";
#Elimnate the drive and check to see if path exists
if(!/\:/ and !-e $path) {
#If not then make it
mkdir $path;
#Show status for demo
print "Makeing $path\n";
}
}
}
perlmonks.org content © perlmonks.org and adamk, Anonymous Monk, BrowserUk, Corion, jZed, snowhare, Three, wolv
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03