Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
9
Task/Make-directory-path/00DESCRIPTION
Normal file
9
Task/Make-directory-path/00DESCRIPTION
Normal 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.
|
||||
2
Task/Make-directory-path/00META.yaml
Normal file
2
Task/Make-directory-path/00META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: File System Operations
|
||||
5
Task/Make-directory-path/Clojure/make-directory-path.clj
Normal file
5
Task/Make-directory-path/Clojure/make-directory-path.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defn mkdirp [path]
|
||||
(let [dir (java.io.File. path)]
|
||||
(if (.exists dir)
|
||||
true
|
||||
(.mkdirs dir))))
|
||||
1
Task/Make-directory-path/Go/make-directory-path.go
Normal file
1
Task/Make-directory-path/Go/make-directory-path.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
os.MkdirAll("/tmp/some/path/to/dir", 0770)
|
||||
14
Task/Make-directory-path/Java/make-directory-path.java
Normal file
14
Task/Make-directory-path/Java/make-directory-path.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Task/Make-directory-path/JavaScript/make-directory-path.js
Normal file
27
Task/Make-directory-path/JavaScript/make-directory-path.js
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
mkdirp[path_] := Quiet[CreateDirectory[path,{CreateIntermediateDirectories->True}],{CreateDirectory::filex}]
|
||||
1
Task/Make-directory-path/Perl-6/make-directory-path.pl6
Normal file
1
Task/Make-directory-path/Perl-6/make-directory-path.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
mkdir 'path/to/dir'
|
||||
23
Task/Make-directory-path/Python/make-directory-path-1.py
Normal file
23
Task/Make-directory-path/Python/make-directory-path-1.py
Normal 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
|
||||
7
Task/Make-directory-path/Python/make-directory-path-2.py
Normal file
7
Task/Make-directory-path/Python/make-directory-path-2.py
Normal 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
|
||||
2
Task/Make-directory-path/Python/make-directory-path-3.py
Normal file
2
Task/Make-directory-path/Python/make-directory-path-3.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def mkdirp(path):
|
||||
os.makedirs(path, exist_ok=True)
|
||||
1
Task/Make-directory-path/README
Normal file
1
Task/Make-directory-path/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Make_directory_path
|
||||
5
Task/Make-directory-path/REXX/make-directory-path.rexx
Normal file
5
Task/Make-directory-path/REXX/make-directory-path.rexx
Normal 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.*/
|
||||
22
Task/Make-directory-path/Racket/make-directory-path.rkt
Normal file
22
Task/Make-directory-path/Racket/make-directory-path.rkt
Normal 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)
|
||||
2
Task/Make-directory-path/Ruby/make-directory-path.rb
Normal file
2
Task/Make-directory-path/Ruby/make-directory-path.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require 'fileutils'
|
||||
FileUtils.mkdir_p("path/to/dir")
|
||||
|
|
@ -0,0 +1 @@
|
|||
new java.io.File("/path/to/dir").mkdirs
|
||||
|
|
@ -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"))
|
||||
1
Task/Make-directory-path/Tcl/make-directory-path.tcl
Normal file
1
Task/Make-directory-path/Tcl/make-directory-path.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
file mkdir ./path/to/dir
|
||||
|
|
@ -0,0 +1 @@
|
|||
function mkdirp() { mkdir -p "$1"; }
|
||||
Loading…
Add table
Add a link
Reference in a new issue