delete non-empty folder

Started by csfreebird, September 06, 2014, 12:15:54 AM

Previous topic - Next topic

csfreebird

Because remove-dir only deletes empty folder, in real word, it cannot offer many help. For the most part, we need to delete a non-empty folder.

I wrote delete-dir function for solving this. But want to ask Lutz, why don't you offer this kind of features?



(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))
      )
    )
)


For testing my code, like so:



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


> (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

rrq

#1
You might want to exclude traversing ".." as well?

For myself, I'm happy enough with the "rm -r" shell command.

csfreebird

#2
.. has been excluded. All file  name ended with . will be excluded.

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$ ll x
total 20
drwxrwxr-x  3 dean dean  4096 Sep  7 14:16 ./
drwxr-xr-x 14 dean dean 12288 Sep  7 14:16 ../
-rw-rw-r--  1 dean dean     0 Sep  7 14:16 a.x
-rw-rw-r--  1 dean dean     0 Sep  7 14:16 .sss
drwxrwxr-x  3 dean dean  4096 Sep  7 14:16 x2/


> (directory "/home/dean/Downloads/x" {[^(.$)]})
("a.x" "x2" ".sss")


rrq

#3
Hmm; are you sure that's the right pattern? Also, doesn't "directory" need a third argument for a regex pattern?



What about a more explicit exclusion, as in the following?
(clean (fn (p) (member p '("." ".."))) (directory dir-path))

csfreebird

#4
You are right. Thank you. :)