s c h e m a t i c s : c o o k b o o k

/ Cookbook.FileCreateDeepDirectory

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Creating Directories Including Parent Directories

Problem

You want to be able to create a directory with something more convenient than make-directory

Solution

make-directory will create a directory, but the parent of that directory must already exist. If you want to be able to create a directory and not worry about whether the parent directory exists, you need to write a little code:

(define (make-directories path)
  (letrec ((normpath (simplify-path path))
           ; assumes that path does not exist.  Caller should
           ; verify that before calling.
           (make-directory-rec 
            (lambda (normpath)
              (let-values (((parent leaf ign) (split-path normpath)))
                (when (not (directory-exists? parent))
                    (make-directory-rec parent))
                (make-directory normpath)))))
    (if (or (directory-exists? normpath)
            (file-exists? normpath))
            (error "Cannot make directory:" path "already exists.")
       (make-directory-rec normpath))))

Discussion

make-directory-rec is where all the real work is, the rest of this is error checking. make-directory-rec splits the path given into a parent and leaf node. If the parent doesn't exist, make-directory-rec calls itself recursively. Then it creates the leaf directory.


Comments about this recipe

Contributors

-- GordonWeakliem - 13 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest