Simple delete directory tree?

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 27 18:08:17 PDT 2013


On Friday, April 26, 2013 17:01:43 Nick Sabalausky wrote:
> On Fri, 26 Apr 2013 22:55:36 +0200
> 
> "Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote:
> > On Friday, 26 April 2013 at 20:54:41 UTC, Nick Sabalausky wrote:
> > > Does phobos have a simple way to delete a directory tree?
> > > std.file.rmdir(path) and std.file.remove(path) don't seem to do
> > > it.
> > 
> > Try rmdirRecurse
> 
> I don't know how I managed to overlook that! Thanks :)
> 
> Unfortunately, that's still choking with "Access is denied" on
> something inside a ".git" subdirectory.

That's an interesting problem to solve. If you can't legally remove a file, 
then rmdirRecurse either has to throw or ignore it. If it ignores it, the only 
way to know what went wrong would be to loop through the files that were 
removed (which could also blow up depending on the file permissions) and see 
what happens when you try and call remove or rmdir on them directly. On the 
other hand, if it throws, then you're basically forces to re-implement 
rmdirRecurse yourself and make it delete every file that you can if you want to 
at least delete the files that you can delete. Neither approach is exactly 
ideal.

Maybe rmdirRecurse should be changed so that it ignores exceptions and returns 
whether it succeeded at removing everything or not (since it's void right 
now). However, if it _did_ fail due to a permissions issue, odds are that it 
would fail a _lot_ (especially if you're talking about a directory that you 
don't have permission to write to), and that would be very expensive. So, that 
might not be a good idea. We _could_ add another argument for telling it which 
behavior you wanted though. But I'm not sure that providing options like that 
is a good idea in the general case, particularly when you start considering 
all of the combinations of behaviors that functions like this could have. 
Still, it may be merited in this case. I don't know.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list