2016-08-14 18 views
0

単純なタスクのように聞こえるかもしれません。しかし、私はちょうどこのアプローチに来た - 良い方法がありますか?clojure:ディレクトリの内容を別のディレクトリにコピーする

(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

は非常に簡単かつ迅速でなければなりませんが

(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))) 
関連する問題