Extended remove-dir ?

Started by DrDave, June 20, 2008, 10:05:00 AM

Previous topic - Next topic

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

Lutz

#1
On Windows:


(define (rmdir dirname)
(exec  (append "rmdir /S " dirname) "Yn"))

DrDave

#2
Quote from: "Lutz"On Windows:


(define (rmdir dirname)
(exec  (append "rmdir /S " dirname) "Yn"))


Ah, but that is not cross-platform, is it?



Anyway, I would probably more often use quiet mode:
(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:


(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

Lutz

#3
QuoteAh, but that is not cross-platform, is it?


On Unix you would use (exec (append "rm -rf " dirname))


QuoteWhy 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.

DrDave

#4
Quote from: "Lutz"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.
...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