Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 524 posts at DZone. You can read more from them at their website. View Full User Profile

Clojure: Create a Directory

05.26.2014
| 6332 views |
  • submit to reddit

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 (java.io.File. "/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 (java.io.File. (str "data/members-" date)))))

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

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)