Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

View file

@ -0,0 +1,9 @@
Create a directory and any missing parents.
This task is named after the posix <code>[http://www.unix.com/man-page/POSIX/0/mkdir/ mkdir -p]</code> command, and several libraries which implement the same behavior.
Please implement a function of a single path string (for example <code>./path/to/dir</code>) 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.

View file

@ -0,0 +1,2 @@
---
note: File System Operations

View file

@ -0,0 +1,5 @@
(defn mkdirp [path]
(let [dir (java.io.File. path)]
(if (.exists dir)
true
(.mkdirs dir))))

View file

@ -0,0 +1 @@
os.MkdirAll("/tmp/some/path/to/dir", 0770)

View file

@ -0,0 +1,14 @@
import java.io.File;
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) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,27 @@
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);
});
break;
// In the case of any other error, something is borked.
default:
cb(er);
break;
}
});
}

View file

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

View file

@ -0,0 +1 @@
mkdir 'path/to/dir'

View file

@ -0,0 +1,23 @@
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):
try:
mkdirp(head, mode)
except OSError as e:
# be happy if someone already created the path
if e.errno != EEXIST:
raise
if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists
return
try:
mkdir(path, mode)
except OSError as e:
# be happy if someone already created the path
if e.errno != EEXIST:
raise

View file

@ -0,0 +1,7 @@
def mkdirp(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise

View file

@ -0,0 +1,2 @@
def mkdirp(path):
os.makedirs(path, exist_ok=True)

View file

@ -0,0 +1 @@
Data source: http://rosettacode.org/wiki/Make_directory_path

View file

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

View file

@ -0,0 +1,22 @@
#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)))
(report-path-exists)
;; Really ... this is the only bit that matters!
(make-directory* path-str)
(report-path-exists)

View file

@ -0,0 +1,2 @@
require 'fileutils'
FileUtils.mkdir_p("path/to/dir")

View file

@ -0,0 +1 @@
new java.io.File("/path/to/dir").mkdirs

View file

@ -0,0 +1,6 @@
import java.io.File
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"))

View file

@ -0,0 +1 @@
file mkdir ./path/to/dir

View file

@ -0,0 +1 @@
function mkdirp() { mkdir -p "$1"; }