Clojure list files from folders and append to file

Consider this situation: you have a lot of files scattered in different folders, you may also constantly move the files around, categorize them, rename them to tidy your folder structure.

One problem may arise is you will lost track of the files you want to locate. A possible solution for this problem is list all files from all the sources and append to a big text file, then open the file with your favorite editor like Emacs and perform query and search in that file. For example search the big file list with multi-occur command in Emacs.

Prepare two bat files

ls.bat

 
@echo off
dir /od /b %*
 

lss.bat

 
ls /s %*
 

Execute shell command from Clojure REPL Executing shell command in Clojure REPL.

The full script is here

 
(def file-name-sources [
"D:\\Download"
"D:\\magazine"
"D:\\incoming"
"H:\\data\\incoming"
"H:\\data\\books"
])
 
(defn list-to-lined-string [lst]
  (join "\n" (rest (rest lst )))
)
 
(defn string-list-files-rec [src]
  (list-to-lined-string (shell "lss.bat" src))
)
 
(defn append-to-file
  "Uses spit to append to a file specified with its name as a string, or
   anything else that writer can take as an argument.  s is the string to
   append."     
  [file-name s]
  (spit file-name s :append true))
 
(defn  generate-big-list [sources out-file]
  (spit out-file "" :append false)
  (for [s sources]
 
    (do
      (println "appending from source:" s)
      (append-to-file out-file  (string-list-files-rec s)))
  )
)
 
(defn refresh-file-list []
  (generate-big-list file-name-sources "c:\\tmp\\filelist.txt")
)