Zero Memory DLL
Zero Memory DLL

Reputation: 149

RemoveDirectory() if the directory is not empty?

On the page (RemoveDirectory() - function) they say : "The path of the directory to be removed. This path must specify an empty directory, and the calling process must have delete access to the directory."

My question is : How do I delete an non-empty directory using this function ? Is that posible ? Any help will be apreciate !

Upvotes: 2

Views: 6482

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490728

The short answer is that you don't.

If you need to remove a non-empty directory, you do a depth-first traversal of the directory structure. As you traverse an individual directory, you erase all the files it contains. If it contains any sub-directories, you traverse into them, deleting their contents then deleting the (now empty) directory. Lather, rinse, repeat.

Alternatively, use SHFileOperation or (if you're feeling truly masochistic) IFileOperation, to handle the heavy lifting for you (but beware that the latter is a COM interface, so getting it to handle the heavy lifting will be at least twice as much work as doing the job yourself).

Upvotes: 4

Related Questions