歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> newLISP 刪除目錄

newLISP 刪除目錄

日期:2017/3/1 9:40:25   编辑:Linux編程

newLISP實現刪除目錄這是個很簡單的需求,可是API只提供了一個幾乎沒什麼用的函數,remove-dir 要求目錄必須為空, 99%的情況是目錄不為空。

因此我寫了一個函數來遞歸刪除目錄樹:

(define (make-sure-folder-path-end-of-slash dir-path)
(if (!= (last dir-path) "/")
(push "/" dir-path -1)
)
dir-path
)

(define (no-sub-files dir-path)
(not (directory dir-path {[^(\.$)]})))

(define (delete-dir dir-path)
;; check dir-path
(unless (directory? dir-path)
(throw-error (string dir-path " folder does not exist")))

;; append slash to dir-path
(set 'dir-path (make-sure-folder-path-end-of-slash dir-path))

;; process sub files
(let (sub-files (directory dir-path {[^(\.$)]}))
(if sub-files
(begin
;; iterate all sub files
(dolist (nde sub-files)
(if (directory? (append dir-path nde))
(delete-dir (append dir-path nde) file-op ext-context)
(let (file-path (append dir-path nde))
(println (string "delete file " file-path ": " (file-info file-path)))
(delete-file file-path ext-context))))
(if (no-sub-files dir-path)
(begin
(println (string "delete folder " dir-path ": " (file-info dir-path)))
(remove-dir dir-path))
)
)
(begin
(println "no sub files in " dir-path " folder, delete this folder")
(remove-dir dir-path))
)
)
)

測試方法:

dean@dean-Latitude-3330:~/Downloads$ mkdir -p x/x2/x3; touch x/x2/x3/z;touch x/x2/m;touch x/.sss; touch x/a.x;
dean@dean-Latitude-3330:~/Downloads$ tree x -a
x
├── a.x
├── .sss
└── x2
├── m
└── x3
└── z

2 directories, 4 files

然後執行newlisp函數:

> (delete-dir "/home/dean/Downloads/x")
delete file /home/dean/Downloads/x/a.x: (0 33204 0 1000 1000 1409987071 1409987071 1409987071)
delete file /home/dean/Downloads/x/x2/x3/z: (0 33204 0 1000 1000 1409987071 1409987071 1409987071)
delete folder /home/dean/Downloads/x/x2/x3/: (4096 16893 0 1000 1000 1409987075 1409987075 1409987075)
delete file /home/dean/Downloads/x/x2/m: (0 33204 0 1000 1000 1409987071 1409987071 1409987071)
delete folder /home/dean/Downloads/x/x2/: (4096 16893 0 1000 1000 1409987075 1409987075 1409987075)
delete file /home/dean/Downloads/x/.sss: (0 33204 0 1000 1000 1409987071 1409987071 1409987071)
delete folder /home/dean/Downloads/x/: (4096 16893 0 1000 1000 1409987075 1409987075 1409987075)
true

為Emacs配置newLISP開發環境 http://www.linuxidc.com/Linux/2013-01/78463.htm

newLISP做GitLab系統備份 http://www.linuxidc.com/Linux/2013-01/78464.htm

newLISP 遍歷目錄樹,清理編譯目錄 http://www.linuxidc.com/Linux/2013-08/88954.htm

newLISP 的詳細介紹:請點這裡
newLISP 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved