Skip to content

Instantly share code, notes, and snippets.

@zk
Created August 5, 2010 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zk/509311 to your computer and use it in GitHub Desktop.
Save zk/509311 to your computer and use it in GitHub Desktop.
(defmacro with-temp-dir [dir & body]
`(when-let [temp-file# (java.io.File/createTempFile (.toString (java.util.UUID/randomUUID)) "")]
(.delete temp-file#)
(.mkdirs temp-file#)
(when-let [temp-dir# (.getAbsolutePath temp-file#)]
((fn [~dir]
(let [res# (do ~@body)]
(clojure.contrib.io/delete-file-recursively temp-dir#)
res#)) temp-dir#))))
;; Usage
(with-temp-dir tmp
(println (str "Temp Directory: " tmp ", will be deleted when this function returns...")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment