首页 > 代码库 > newLISP文件合并

newLISP文件合并

需求是几百个文件,命名规则类似下面的文件名:

dailydata_20140320.txt

通过newLISP合并,首先是自动生成这些文件名,然后用read-file读取内容,再用append-file写入到一个文件中。

注意删除上次运行的结果,也要注意有文件不存在的话需要出error.log日志。

代码如下:

#!/usr/bin/newlisp

(set ‘start-date "20140101 00:00")
(set ‘start-seconds (date-parse start-date "%Y%m%d %H:%M"))
(set ‘day-seconds (* 3600 24))

(if (file? "20140101_20140828.txt")
    (delete-file "20140101_20140828.txt"))

(if (file? "error.log")
    (delete-file "error.log"))

(set ‘x 0)
(while (< x 240)
  (begin
   (set‘ compute-date (date (+ (* day-seconds x) start-seconds) 0 "%Y%m%d"))
   (set ‘file-name (string "dailydata_" compute-date ".txt"))
   (if (file? file-name)
       (begin
        (set ‘file-content (read-file file-name))
        (print file-content)
        (append-file "20140101_20140828.txt" file-content)
        )
     (append-file "error.log" (string file-name " does not exist")))
   (inc x)
  ))

(exit)


newLISP文件合并