how can I make a directory that includes subdirectories however many levels deep?
like this:
> (make-dir "one")
true
> (make-dir "one/two")
true
> (make-dir "one/two/three")
true
>
Lutz
I'm sorry, I meant all at once, something analogous to
$ mkdir -p one/two/three
...something that makes the deepest subdirectory and
all its parents whether or not the parent existed
previously.
this will work:
(exec "mkdir -p one/two/three")
Lutz
thanks.
That's sort of cheating, though!
:-)
QuoteThat's sort of cheating, though!
shhh, don't let anybody know, and here is the pure and portable thing:
(define (makedir path)
(let (old-path (real-path))
(dolist (p (parse path "/"))
(make-dir p)
(change-dir p))
(change-dir old-path)))
(makedir "one/two/three")
Lutz
hi Lutz,
hmm. the "correct" version doesn't seem to work today. For now, I
commented out your function, and am using the "cheating" method.
I'll go ahead and post my whole complex, lengthy, and guru-like script
(which will also change the way we use our computers forever). Here
it is:
#!/usr/bin/newlisp
;; newlisp implementation of "mkdir -p" (thanks Lutz!)
; (define (make-sub-dirs path)
; (let (old-path (real-path))
; (dolist (p (parse path "/"))
; (make-dir p)
; (change-dir p))
; (change-dir old-path)))
;; split up the date
(set
'the-year (date (date-value) 0 "%Y")
'the-month (date (date-value) 0 "%b")
'the-day (date (date-value) 0 "%d")
'the-time (date (date-value) 0 "%H:%M:%S")
)
;; create directories, subdirectories, file name
(set
'base-dir-path "/path/to/dir"
'text-dir-path
(append base-dir-path "/" the-year "/" the-month "/" the-day"/")
'file-name
(append (date (date-value) 0 "%H%M%S")".txt")
)
;(make-sub-dirs text-dir-path)
(exec (append "mkdir -p " text-dir-path))
;; write stuff to file
(device (open (append text-dir-path file-name) "write"))
(print "-*- Outline -*-nn")
(print (append "* " (string (date)) "nn"))
(close (device))
;; launch editor with file
(! (append "gnuclient " (append text-dir-path file-name)))
;; quit :-)
;(exit)
The other simple native version
(define (mkdirs path , p)
(dolist (l (parse path "/"))
(push l p -1)
(unless (empty? l) (make-dir (join p "/")))))
example: (mkdirs "the/test/dir") or (mkdirs "/the/test/dir")
this one even returns nil on error :-)
...just a coding exercise