Skip to content

Instantly share code, notes, and snippets.

@tueda
Created December 3, 2013 15:33
Show Gist options
  • Save tueda/7771237 to your computer and use it in GitHub Desktop.
Save tueda/7771237 to your computer and use it in GitHub Desktop.
Ensures the given path is created. For Mathematica 7+.
(*
* Makes a directory tree.
*)
EnsurePath[path_String] := Module[{names, cd, p},
names = FileNameSplit[path];
cd = {};
While[Length[names] > 0,
AppendTo[cd, First[names]];
names = Rest[names];
p = FileNameJoin[cd];
If[!DirectoryQ[p],
If[CreateDirectory[p] === $Failed,
Return[$Failed];
];
];
];
path
];
@tueda
Copy link
Author

tueda commented Jan 23, 2014

It turned out that If[!DirectoryQ["path"],CreateDirectory["path"]] is enough due to the default setting CreateIntermediateDirectories->True.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment