make-dirs

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

make-dirs

Post by tom »

how can I make a directory that includes subdirectories however many levels deep?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

like this:

Code: Select all

> (make-dir "one")
true
> (make-dir "one/two")
true
> (make-dir "one/two/three")
true
> 
Lutz

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

I'm sorry, I meant all at once, something analogous to

Code: Select all

 $ mkdir -p one/two/three 
...something that makes the deepest subdirectory and
all its parents whether or not the parent existed
previously.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

this will work:

Code: Select all

(exec "mkdir -p one/two/three")
Lutz

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

thanks.

That's sort of cheating, though!

:-)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

That's sort of cheating, though!
shhh, don't let anybody know, and here is the pure and portable thing:

Code: Select all

(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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

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:

Code: Select all

#!/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 -*-\n\n")
(print (append "* " (string (date)) "\n\n"))
(close (device))

;; launch editor with file

(! (append "gnuclient " (append text-dir-path file-name)))

;; quit :-)

;(exit)


Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

The other simple native version

Code: Select all

(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
WBR, Dmi

Locked