delete non-empty folder

Q&A's, tips, howto's
Post Reply
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

delete non-empty folder

Post by 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?

Code: Select all

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

Code: Select all

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

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: delete non-empty folder

Post by ralph.ronnquist »

You might want to exclude traversing ".." as well?
For myself, I'm happy enough with the "rm -r" shell command.

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: delete non-empty folder

Post by csfreebird »

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

Code: Select all

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


ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: delete non-empty folder

Post by ralph.ronnquist »

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?

Code: Select all

(clean (fn (p) (member p '("." ".."))) (directory dir-path))

csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

Re: delete non-empty folder

Post by csfreebird »

You are right. Thank you. :)

Post Reply