Perl API that emulates mkdir -p on unix?
Anonymous Monk
created: 2004-06-13 14:17:38
Hi Monks: Is there any perl API that emulates mkdir -p on unix, meaning creates directories recursively. Simply, mkdir $dirname doesn't work. Thanks a lot for your help. Rgds, Tony
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 14:20:57
File::Path (part of core) has mkpath()
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 14:21:19

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.

Re^2: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 21:06:09
However, be aware that File::Path does not play nice with taint. I had to 'roll my own' in-house equivalent because of this.
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 14:26:40
Path::Class is also capable of this, if you prefer an OO interface. See its documentation on the $dir->mkpath method.
use Path::Class qw(dir);
dir("foo", "bar")->mkpath;
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 18:25:14
what's wrong with ...
system( "mkdir -p $dirname" );
Re^2: Perl API that emulates mkdir -p on unix?
created: 2004-06-13 18:52:29
What about platforms that do not have mkdir? Why rely on external tools at all? That just creates a useless dependency that can't be satisfied on all platforms.
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-14 08:52:58
Might be a little heavier that you want, but File::Flat also does this, or rather it lets you just write a file somewhere and it sorts all the details out for you.
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".

Great for writing installer-type applications.
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-14 09:47:19
$path =~ s[/][\\]g;
system "md $path";
Re: Perl API that emulates mkdir -p on unix?
created: 2004-06-14 17:23:45

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