听起来像一个简单的任务,也许就是这样。但是我刚刚采用这种方法 - 有更好的方法吗?
(require '[clojure.string :as string])
(defn mk-path [& args]
(string/join "/" args))
(defn move-contents-to-dir [dir1 dir2]
"Move all the contents of dir1 into dir2, which will be created if not existing."
(doseq [file (file-seq (io/file dir1))]
(let [base-removed (string/replace (str file)
(re-pattern (str "^" (str dir1)))
"")
new-path (mk-path dir2 base-removed)]
(io/make-parents new-path)
(when (not (.isDirectory file))
(io/copy file (io/file new-path))))))
答案 0 :(得分:0)
有一个名为fs的方便库,可以实现一些你可以使用的帮助: http://raynes.github.io/fs/me.raynes.fs.html#var-copy-dir
答案 1 :(得分:0)
应该非常简单和快速
(ns hello-world.core
(:import [java.nio.file Files Paths SimpleFileVisitor StandardCopyOption FileVisitResult LinkOption CopyOption]
[java.nio.file.attribute FileAttribute]))
(defn path [str-path]
(Paths/get str-path (into-array String [])))
(defn copy-dir-visitor [from to]
(proxy [SimpleFileVisitor] []
(preVisitDirectory [dir attrs]
(let [target (.resolve to (.relativize from dir))]
(if-not (Files/exists target (into-array LinkOption []))
(Files/createDirectory target (into-array FileAttribute [])))
FileVisitResult/CONTINUE))
(visitFile [file attrs]
(let [target (.resolve to (.relativize from file))]
(Files/copy file target (into-array CopyOption [StandardCopyOption/REPLACE_EXISTING])))
FileVisitResult/CONTINUE)))
(defn copy-dir! [from to]
(let [visitor (copy-dir-visitor from to)]
(Files/walkFileTree from visitor)))