你好,游客 登录 注册 搜索
背景:
阅读新闻

newLISP 删除目录

[日期:2014-09-06] 来源:Linux社区  作者:csfreebird [字体:大 中 小]

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 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/106263.htm

linux
相关资讯       newLISP  newLISP删除目录 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款