Clojure整理文件
最近要回家了,需要把资料整理整理,在电脑上有一大堆书籍,分类存放在各自所属的目录下,要整理起来还挺麻烦的,偷懒是人类进步的伟大特质之一,用Clojure完成这个任务吧。
写完以后效果不错,完成这一工作只用了不到20行代码,由于我是新手,应该还可以写得更简练的,纯当练手了。代码如下:
(ns ToolsUtilities.core (:use [clojure.java.io])) (def target-types ["pdf"]) (defn file-filt-pred? [file] (let [file-name (.getName file)] (some #(= (.substring file-name (+ 1 (.lastIndexOf file-name "."))) %) target-types))) (defn move-subfile-to-target [source target file-filt-pred] (.mkdir (file target)) (let [files (filter file-filt-pred (filter #(.isFile %)(file-seq (file source))))] (doseq [per-file files] (copy per-file (file (str target "/" (.getName per-file))))))) (defn collect-book [source-path target-path] (let [dir-filter (partial filter #(.isDirectory %)) dirs (-> source-path file .listFiles dir-filter)] (doseq [dir dirs] (move-subfile-to-target dir (str target-path "/" (.getName dir)) file-filt-pred?))))