Page 1 of 1

Extended remove-dir ?

Posted: Fri Jun 20, 2008 5:05 pm
by DrDave
I see that remove-dir returns nil if either the directory is not empty or if the directory does not exist. If there is not already a built-in function that will remove a directory and its contents and all sub-directories, then could remove-dir be extended to somethig like:

(remove-dir str-path [bool])

If the optional bool argument is true, a directory, and all content beneath it, will be deleted. If the directory was not found, nil would be returned.

If it is too much bother or not deemed useful enough, I can certainly write my own function that includes testing for the existence of the directory as well as checking if it is empty and acting as I desire.

Posted: Fri Jun 20, 2008 6:23 pm
by Lutz
On Windows:

Code: Select all

(define (rmdir dirname)
	(exec  (append "rmdir /S " dirname) "Y\n"))

Posted: Fri Jun 20, 2008 8:09 pm
by DrDave
Lutz wrote:On Windows:

Code: Select all

(define (rmdir dirname)
	(exec  (append "rmdir /S " dirname) "Y\n"))
Ah, but that is not cross-platform, is it?

Anyway, I would probably more often use quiet mode:

Code: Select all

(define (rmdir dirname)
	(exec  (append "rmdir /S /Q " dirname) )
Why does the first version return true and the second (quiet) version return () ?

Anyway, that is only half the solution because if the directory does not exist, the retun value is the same as when successful, plus the system throws up an error message. So I added some code to return an error message to me instead of letting the OS throw the error:

Code: Select all

(define (rmdir dirname)
          (if (file? dirname)
   	       (exec  (append "rmdir /S /Q " dirname))
             (println (append dirname " not found"))
           )
)

Posted: Fri Jun 20, 2008 9:02 pm
by Lutz
Ah, but that is not cross-platform, is it?
On Unix you would use (exec (append "rm -rf " dirname))
Why does the first version return true and the second (quiet) version return () ?
The first version supplies stdin input via the second parameter of 'exec'. This is necessary for the /S switch, which causes Win32 to ask for input. In my experiments rmdir /S without supplyung the Y (for yes) input did not remove the directory!

The second version returns a list of stdout lines from shell command. If there is none, than the list is empty.

Posted: Sat Jun 21, 2008 6:07 am
by DrDave
Lutz wrote:In my experiments rmdir /S without supplying the Y (for yes) input did not remove the directory!
It defintely will not remove the directory on Win32 without the Y switch because this command blocks with a user prompt. If the user does not respond with Y or N, it waits forever. The Q switch over-rides this behavior and suppresses the user prompt and continues to execute the command. If successful, there is no feedback to the user. If it fails because the directory does not exist, a message to this effect is displayed at the command prompt. I don't want the system to have to handle this error condition, so I added the test for the existence of the directory that enables me to decide how to handle this condition.