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

/ FileAndDirectoryRecipes? / Cookbook.FileMaybeMakeDirectory

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

Making A Directory If It Doesn't Already Exist

Problem

You want to create a directory only if it doesn't already exist.

Solution

Use make-directory* from the file.ss module in Mzlib. For example:

> (require (lib "file.ss"))
> (directory-exists? "foo") ; show the directory doesn't exist
#f
> (make-directory* "foo")   ; create the directory
> (directory-exists? "foo") ; show the directory exists
#t
> (make-directory* "foo")   ; no error is raised if the directory already exists

Discussion

The function make-directory* will make all the directories specified by it's argument that don't already exist. So to create the directory tree "foo/bar" (if it doesn't already exist) you could call make-directory* like so:

> (make-directory* "foo/bar")

If you wanted to ensure that only one directory was created, not an entire tree, you could build your own function on top of make-directory. The function make-directory raises an exception if the directory already exists so you have to check for the directory before you attempt to create it. The function maybe-make-directory does this:

(define (maybe-make-directory directory)
  (unless (directory-exists? directory)
      (make-directory directory)))


Comments about this recipe

Contributors

-- NoelWelsh - 18 May 2004

-- DaveHerman - 22 Feb 2005

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder:

 
 
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