Remove dir contents?

Jonathan M Davis jmdavisProg at gmx.com
Thu Apr 5 11:23:42 PDT 2012


On Thursday, April 05, 2012 12:55:23 Andrej Mitrovic wrote:
> On 4/5/12, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> > Otherwise I have to use platform-specific calls to system() like "del
> > *.*".
> 
> Actually nevermind that, I can just remove the dir entirely and then
> re-create it.

I don't believe that there's any system call or command-line program on any of 
the platforms that D supports which specifically deletes the _contents_ of a 
directory - as your del *.* demonstrates for Windows, so I wouldn't expect 
such a thing in Phobos - especially when what's there makes it quite possible 
to do what you want, even if it's not one command.

The normal way to do it would probably be to iterate over the contents and 
delete everything one by one. Your proposed solution or removing the dir and 
then creating it again is arguably cleaner, but it does have the downside of 
the new directory possibly not matching the original one with regards to 
permissions or ownership (it may even be that you don't have permission to 
remove it or create it even if you have permission to edit to its contents, 
since you could have write permission for the directory but not for its parent 
directory). I don't think that that's really an issue in Windows (which you're 
presumably using given your suggestion of del *.*), since it's pretty limited 
when it comes to permissions, but it definitely would be on Posix systems.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list