Mark Needham

Thoughts on Software Development

Clojure: Create a directory

with 4 comments

I spent much longer than I should have done trying to work out how to create a directory in Clojure as part of an import script I’m working out so for my future self this is how you do it:

(.mkdir ( "/path/to/dir/to/create"))

I’m creating a directory which contains today’s date so I’d want something like ‘members-2014-05-24’ if I was running it today. The clj-time library is very good for working with dates.

To create a folder containing today’s date this is what we’d have:

(ns neo4j-meetup.core
  (:require [clj-time.format :as f]))
(def format-as-year-month-day (f/formatter "yyyy-MM-dd"))
(defn create-directory-for-today []
  (let [date (f/unparse format-as-year-month-day (t/now))]
    (.mkdir ( (str "data/members-" date)))))

Initial code shamelessly stolen from Shu Wang’s gist so thanks to him as well!

Be Sociable, Share!

Written by Mark Needham

May 24th, 2014 at 12:12 am

Posted in Clojure

Tagged with

  • Denis Fuenzalida

    Note that there are times in which you want to create more than just one directory, but a whole path with all the intermediate folders. In that case, use mkdirs (see

  • Duck

    Learned the hard way that .mkdir and .mkdirs swallow errors. They return false both when the directory already exists and if there’s some error like permissions. Not sure how to get the error.

  • Curt

    If you’re doing very much file/directory work, I recommend using “fs” for a nice clojure wrapper around the java constructs:

  • @Curt – nice one, I hadn’t come across that in my search. Should use that next time 🙂