Create Directory

See also String to List

Function Syntax (LM:CreateDirectory <dir>)
Current Version 1.2
Donate
Arguments
Symbol Type Description
dir String Directory to create e.g. ("C:/Folder1/Folder2")
Returns
Type Description
Boolean T if directory creation is successful, else nil

Function Description

This function will recursively create each level of a directory folder structure, if not already existing.

When called with a string representing a directory folder structure, the function will recursively create each level of the folder structure using the Visual LISP vl-mkdir function. The function will subsequently return T should the supplied directory path exist following evaluation.

The function will create any folders which are not present at each level of the directory structure, and will not error should a folder in the structure already exist. For example, should the user call the subfunction with:

(LM:createdirectory "C:\\Folder1\\Folder2\\Folder3")

If 'Folder1' & 'Folder2' already exist in the folder structure, the function will still proceed to create 'Folder3'.

Select all
;; Create Directory  -  Lee Mac
;; dir - [str] directory to create ("C:\\Folder1\\Folder2")
;; Returns:  T if directory creation is successful, else nil

(defun LM:createdirectory ( dir )
    (   (lambda ( fun )
            (   (lambda ( lst ) (fun (car lst) (cdr lst)))
                (vl-remove "" (LM:str->lst (vl-string-translate "/" "\\" dir) "\\"))
            )
        )
        (lambda ( root lst / dir )
            (if lst
                (if (or (vl-file-directory-p (setq dir (strcat root "\\" (car lst)))) (vl-mkdir dir))
                    (fun dir (cdr lst))
                )
            )
        )
    )
    (vl-file-directory-p dir)
)

;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] string to process
;; del - [str] delimiter by which to separate the string

(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

Example Function Call

Note that the supplied directory path may contain either forward slash path delimiters ("/") or double-backslashes ("\\"), furthermore, the presence of a trailing slash will have no effect on directory creation.

(LM:createdirectory "C:\\Folder1\\Folder2\\Folder3")

See also String to List

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010