-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmerge.bb
More file actions
executable file
·36 lines (30 loc) · 1.09 KB
/
merge.bb
File metadata and controls
executable file
·36 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env bb
(require '[clojure.java.io :as io]
'[clojure.string :as str])
(def output "all.md")
(def separator "\n%\n")
;; 1. 取得符合 "day*.md" 模式的檔案列表
;; - file-seq 會遞迴地列出檔案,但我們只取頂層檔案。
;; - filter 找出檔名符合 "day*.md" 模式的檔案。
(def files
(->> (io/file ".") ; 從當前目錄開始
(file-seq)
(filter #(.isFile %)) ; 只保留檔案
(filter #(re-matches #"day.*\.md" (.getName %))) ; 找出 day*.md
(sort) ; 依檔名排序 (可選)
(map #(.toString %)))) ; 轉成字串路徑列表
;; 2. 準備要寫入的內容
;; - 使用 map 讀取每個檔案內容,並用 separator 分隔。
(def content
(str/join
separator
(map (fn [file-path]
(try
(slurp file-path)
(catch Exception e
(println (str "Error reading file: " file-path))
"")))
files)))
;; 3. 寫入到輸出檔案 (會覆蓋)
(spit output content)
(println (str "Successfully merged " (count files) " files into " output))