如何在Clojure中复制目录?

Zub*_*air 1 clojure

是否有一个库允许我在 Clojure 中复制目录和所有子目录?就像是:

(copy "source-dir" "destination-dir")

Sté*_*ert 5

您可以使用以下代码copy-dir

复制目录(从到复制目录)

将目录从 复制fromto. 如果 to已存在,则将该目录复制到与fromto目录内同名的目录中。

(defn copy-dir
  "Copy a directory from `from` to `to`. If `to` already exists, copy the directory
   to a directory with the same name as `from` within the `to` directory."
  [from to]
  (when (exists? from)
    (if (file? to)
      (throw (IllegalArgumentException. (str to " is a file")))
      (let [from (file from)
            to (if (exists? to)
                 (file to (base-name from))
                 (file to))
            trim-size (-> from str count inc)
            dest #(file to (subs (str %) trim-size))]
        (mkdirs to)
        (dorun
         (walk (fn [root dirs files]
                 (doseq [dir dirs]
                   (when-not (directory? dir)
                     (-> root (file dir) dest mkdirs)))
                 (doseq [f files]
                   (copy+ (file root f) (dest (file root f)))))
               from))
        to))))
Run Code Online (Sandbox Code Playgroud)

或者直接使用Github 上提供的fs 库

  • @Zubair 根据 http://stackoverflow.com/help/on-topic 要求图书馆明确偏离主题 (2认同)
  • @developerbmw 没问题。不管怎样,现在问题解决了。我本来会删除这个问题,但由于 Stephane 花时间回答它,我暂时将它留在那里 (2认同)
  • @DavidShaked你会在这里找到其他定义https://github.com/Raynes/fs/blob/master/src/me/raynes/fs.clj#L101 (2认同)