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.
Extended remove-dir ?
Extended remove-dir ?
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2
On Windows:
Code: Select all
(define (rmdir dirname)
(exec (append "rmdir /S " dirname) "Y\n"))
Ah, but that is not cross-platform, is it?Lutz wrote:On Windows:
Code: Select all
(define (rmdir dirname) (exec (append "rmdir /S " dirname) "Y\n"))
Anyway, I would probably more often use quiet mode:
Code: Select all
(define (rmdir dirname)
(exec (append "rmdir /S /Q " dirname) )
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"))
)
)
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2
On Unix you would use (exec (append "rm -rf " dirname))Ah, but that is not cross-platform, is it?
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!Why does the first version return true and the second (quiet) version return () ?
The second version returns a list of stdout lines from shell command. If there is none, than the list is empty.
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.Lutz wrote:In my experiments rmdir /S without supplying the Y (for yes) input did not remove the directory!
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2
"Getting Started with Erlang" version 5.6.2