CloudFlare suffered a massive security issue affecting all of its customers, including Rosetta Code. All passwords not changed since February 19th 2017 have been expired, and session cookie longevity will be reduced until late March.--Michael Mol (talk) 05:15, 25 February 2017 (UTC)

Make directory path

From Rosetta Code
Make directory path
You are encouraged to solve this task according to the task description, using any language you may know.

Create a directory and any missing parents.

This task is named after the posix mkdir -p command, and several libraries which implement the same behavior.

Please implement a function of a single path string (for example ./path/to/dir) which has the above side-effect. If the directory already exists, return successfully. Ideally implementations will work equally well cross-platform (on windows, linux, and OS X).

It's likely that your language implements such a function as part of its standard library. If so, please also show how such a function would be implemented.


# syntax: GAWK -f MAKE_DIRECTORY_PATH.AWK path ...
for (i=1; i<=ARGC-1; i++) {
path = ARGV[i]
msg = (make_dir_path(path) == 0) ? "created" : "exists"
printf("'%s' %s\n",path,msg)
function make_dir_path(path, cmd) {
# cmd = sprintf("mkdir -p '%s'",path) # Unix
cmd = sprintf("MKDIR \"%s\" 2>NUL",path) # MS-Windows
return system(cmd)

sample command and output under Windows 8:


'\TEMP\A' created
'\TEMP\A' exists
'\TEMP\A\B C' created


#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int main (int argc, char **argv) {
char *str, *s;
struct stat statBuf;
if (argc != 2) {
fprintf (stderr, "usage: %s <path>\n", basename (argv[0]));
exit (1);
s = argv[1];
while ((str = strtok (s, "/")) != NULL) {
if (str != s) {
str[-1] = '/';
if (stat (argv[1], &statBuf) == -1) {
mkdir (argv[1], 0);
} else {
if (! S_ISDIR (statBuf.st_mode)) {
fprintf (stderr, "couldn't create directory %s\n", argv[1]);
exit (1);
s = NULL;
return 0;



Common Lisp[edit]

(ensure-directories-exist "your/path/name")


(defn mkdirp [path]
(let [dir ( path)]
(if (.exists dir)
(.mkdirs dir))))


Tries to create the directory `path`. Missing parent directories are created.



ERRE has the procedure "OS_MKDIR" in PC.LIB standard library, that creates a directory with all missing parents. Existing directory are simply ignored.



The library function System.IO.Directory.CreateDirectory also returns a DirectoryInfo object of the deepest directory in the path.

In the F# REPL:

> System.IO.Directory.CreateDirectory (System.IO.Directory.GetCurrentDirectory())
val it : System.IO.DirectoryInfo =
  Temp {Attributes = Directory;
        CreationTime = 2016-06-01 04:12:25;
        CreationTimeUtc = 2016-06-01 02:12:25;
        Exists = true;
        Extension = "";
        FullName = "C:\Users\Kai\AppData\Local\Temp";
        LastAccessTime = 2016-08-18 20:42:21;
        LastAccessTimeUtc = 2016-08-18 18:42:21;
        LastWriteTime = 2016-08-18 20:42:21;
        LastWriteTimeUtc = 2016-08-18 18:42:21;
        Name = "Temp";
        Parent = Local;
        Root = C:\;}


The standard packages include os.MkdirAll which does exactly this (and its source is also available via that link).

	os.MkdirAll("/tmp/some/path/to/dir", 0770)


import System.Directory (createDirectory, setCurrentDirectory)
import Data.List.Split (splitOn)
main :: IO ()
main = do
let path = splitOn "/" "path/to/dir"
mapM_ (\x -> createDirectory x >> setCurrentDirectory x) path


The verb pathcreate in the addon package general/dirutils will create any non-existing directories in a path. It works on Windows, Linux and OS X.

require 'general/dirutils'
pathcreate '/tmp/some/path/to/dir'

Code is similar to the following:

pathcreate=: monad define
todir=. termsep_j_ jpathsep y
todirs=. }. ,each /\ <;.2 todir NB. base dirs
msk=. -.direxist todirs NB. 1 for each non-existing dir
msk=. 0 (i. msk i: 0)}msk
dircreate msk#todirs NB. create non-existing base dirs
dircreate=: monad define
y=. boxxopen y
msk=. -.direxist y
if. ''-:$msk do. msk=. (#y)#msk end.
res=. 1!:5 msk#y
msk #inv ,res
direxist=: 2 = ftype&>@:boxopen


The Java method for this is mkdirs and can be found in The source is in the of the JDK root directory.

public interface Test {
public static void main(String[] args) {
try {
File f = new File("C:/parent/test");
if (f.mkdirs())
System.out.println("path successfully created");
} catch (Exception e) {


Works with: Node.js

Simplified version of the popular mkdirp library:

var path = require('path');
var fs = require('fs');
function mkdirp (p, cb) {
cb = cb || function () {};
p = path.resolve(p);
fs.mkdir(p, function (er) {
if (!er) {
return cb(null);
switch (er.code) {
case 'ENOENT':
// The directory doesn't exist. Make its parent and try again.
mkdirp(path.dirname(p), function (er) {
if (er) cb(er);
else mkdirp(p, cb);
// In the case of any other error, something is borked.


// version 1.0.6
fun main(args: Array<String>) {
// using built-in mkdirs() method
val success = File("./path/to/dir").mkdirs()
if (success) println("Directory path was created successfully")
else println("Failed to create directory path")
Directory path was created successfully


Creates directory specified by path, creating intermediate directories as necessary, and never fails if path already exists.

mkdirp[path_] := Quiet[CreateDirectory[path,{CreateIntermediateDirectories->True}],{CreateDirectory::filex}]


(define (mkdir-p mypath)
(if (= "/" (mypath 0)) ;; Abs or relative path?
(setf /? "/")
(setf /? "")
(setf path-components (clean empty? (parse mypath "/"))) ;; Split path and remove empty elements
(for (x 0 (length path-components))
(setf walking-path (string /? (join (slice path-components 0 (+ 1 x)) "/")))
(make-dir walking-path)
;; Using user-made function...
(mkdir-p "/tmp/rosetta/test1")
;; ... or calling OS command directly.
(! "mkdir -p /tmp/rosetta/test2")


Using the File::Path core module:

use File::Path qw(make_path);

Perl 6[edit]

Works with: rakudo version 2016.06

There is a built-in function for this:

mkdir 'path/to/dir'

Alternatively, a custom solution (as per task description) that only uses the built-in mkdir non-recursively. The "triangle reduce" meta-operator [\ ] is used get the intermediate results of a left fold with the comma operator on the list of path elements.

for [\,] $*SPEC.splitdir("../path/to/dir") -> @path {
mkdir $_ unless .e given $*SPEC.catdir(@path).IO;


(call "mkdir" "-p" "path/to/dir")


New-Item -Path ".\path\to\dir" -ItemType Directory -ErrorAction SilentlyContinue


from errno import EEXIST
from os import mkdir, curdir
from os.path import split, exists
def mkdirp(path, mode=0777):
head, tail = split(path)
if not tail:
head, tail = split(head)
if head and tail and not exists(head):
mkdirp(head, mode)
except OSError as e:
# be happy if someone already created the path
if e.errno != EEXIST:
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
mkdir(path, mode)
except OSError as e:
# be happy if someone already created the path
if e.errno != EEXIST:

Above is a modified version of the standard library's os.makedirs, for pedagogical purposes. In practice, you would be more likely to use the standard library call:

def mkdirp(path):
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
else: raise

In Python3 this becomes even simpler:

def mkdirp(path):
os.makedirs(path, exist_ok=True)


Uses make-directory* (NB the star — that causes the intermediate directories to be produced).

Canonical documentation at Racket Documentation for Filesystem

Creates directory specified by path, creating intermediate

directories as necessary, and never failing if path exists already.

#lang racket
(define path-str "/tmp/woo/yay")
(define path/..-str "/tmp/woo")
;; clean up from a previous run
(when (directory-exists? path-str)
(delete-directory path-str)
(delete-directory path/..-str))
;; delete-directory/files could also be used -- but that requires goggles and rubber
;; gloves to handle safely!
(define (report-path-exists)
(printf "~s exists (as a directory?):~a~%~s exists (as a directory?):~a~%~%"
path/..-str (directory-exists? path/..-str)
path-str (directory-exists? path-str)))
;; Really ... this is the only bit that matters!
(make-directory* path-str)
"/tmp/woo" exists (as a directory?):#f
"/tmp/woo/yay" exists (as a directory?):#f

"/tmp/woo" exists (as a directory?):#t
"/tmp/woo/yay" exists (as a directory?):#t


The following works with any modern (Microsoft) Windows ® and/or DOS.

Operating system note:   all versions of Microsoft ® DOS require the use of a blackslash   [\]   instead of a forward slash   [/].

Usage note:   without the error messages being suppressed, the   MKDIR   command will issue an error message if the subdirectory (or its path) already exists.

/*REXX program  creates a  directory (folder)  and all its  parent paths  as necessary. */
trace off /*suppress possible warning msgs.*/
dPath = 'path\to\dir' /*define directory (folder) path.*/
'MKDIR' dPath "2>nul" /*alias could be used: MD dPath */
/*stick a fork in it, we're done.*/


System("mkdir C:\Ring\docs")
see isdir("C:\Ring\docs") + nl
func isdir cDir
return true
return false


require 'fileutils'

mkdir_p also takes an array of pathnames instead of a single pathname as an argument. mkdir_p is aliased as: mkpath, makedirs.

Run BASIC[edit]

files #f, "c:\myDocs" ' check for directory
if #f hasanswer() then
if #f isDir() then ' is it a file or a directory
print "A directory exist"
print "A file exist"
end if
shell$("mkdir c:\myDocs" ' if not exist make a directory
end if

The following info about files / directory FILE ACCESSOR methods

  1. handle HASANSWER() - Return non-zero if the file accessor has at least one

resulting row.

  1. handle ROWCOUNT() - Return the number of rows returned.
  2. handle NEXTFILE$() - Advance to the next row and return a comma delimited string for the next file (name, size, date, time, directory flag).
  3. handle NEXTFILE$([delimExpr$]) - Like NEXTFILE$() but you get to specify the delimiter instead of a comma.
  4. handle NAME$() - Return the name of the current file row.
  5. handle SIZE() - Return the size of the current file row.
  6. handle DATE$() - Return a string containing a formatted date for the current file row.
  7. handle TIME$() - Return a string containing a formatted time for the current file row.
  8. handle ISDIR() - Return non-zero if the current file row represents a directory instead of a file.
  9. handle RESET() - Reset the file accessor back to the beginning so you can read through them again.
  10. handle DATEFORMAT(template$) - Set the date format using a "mmm dd, yyyy" style template$.
  11. handle TIMEFORMAT(template$) - Set the time format using a "hh:mm:ss" style template$.
  12. handle ISNULL() - Returns zero (or false)
  13. handle DEBUG$() - Returns the string "Files"


use std::fs;
fn main() {
fs::create_dir_all("./path/to/dir").expect("An Error Occured!")



Alternative (platform-independent) for the library function:

def mkdirs(path: List[String]) = // return true if path was created
path.tail.foldLeft(new File(path.head)){(a,b) => a.mkdir; new File(a,b)}.mkdir
mkdirs(List("/path", "to", "dir"))


The library cli_cmds.s7i defines the function doMkdirCmd, which is used below.

$ include "seed7_05.s7i";
include "cli_cmds.s7i";
const proc: main is func
doMkdirCmd(argv(PROGRAM), TRUE);
end func;

The library cli_cmds.s7i defines also doMkdir (Make directories like the Unix mkdir command) and doMd (Make directories like the DOS md command). This functions read the parameters and options from a string. The reading is done according to Unix respectively DOS/Windows rules. The function doMkdir is used in the alternate solution below:

$ include "seed7_05.s7i";
include "cli_cmds.s7i";
const proc: main is func
var string: parameters is "";
parameters := join(argv(PROGRAM), " ");
end func;

Sidef[edit], "path", "to", "dir").make_path;   # works cross-platform


Tcl's built in file mkdir works exactly this way:

file mkdir ./path/to/dir

If a directory cannot be made (e.g., because it would involve making a directory with the same name as an existing file) the command will throw a trappable error, as normal for Tcl commands.

UNIX Shell[edit]

Works with: Bourne Again SHell
function mkdirp() { mkdir -p "$1"; }


This is for Unix as zkl doesn't have a built in mkdir method.

System.cmd("mkdir -p ../foo/bar")

The system error code is returned (0 in this case).

fcn mkdir(path) { System.cmd("mkdir -p "+path) }
zkl: mkdir("../foo/bar")
zkl: mkdir("/foo/bar")
mkdir: cannot create directory ‘/foo’: Permission denied