Click to See Complete Forum and Search --> : how to delete directory and its content?


Eugene
April 1st, 1999, 05:21 AM
How can I delete the whole directory regardless whether it is empty or not? I've tried RemoveDirectory(), remove() function, but their all failed when there is file or subdirectory in the directory.

eugene
April 1st, 1999, 05:21 AM
How can I delete the whole directory regardless whether it is empty or not? I've tried RemoveDirectory(), remove() function, but their all failed when there is file or subdirectory in the directory.

Zc Wang
April 1st, 1999, 06:34 AM
No Win32 API function can delete the whole directory ! You must implement this

by RemoveDirectory() and DeleteFile().

Jerry Coffin
April 1st, 1999, 12:37 PM
You can do a recursive search, finding and deleting all the contents of the directory before you attempt to delete the directory itself.


Alternatively, you can use SHFileOperation to delete the entire directory at once.

wayned
March 22nd, 2001, 06:43 AM
The easiest way I found was to get my program to create a bat file on the fly that contains the following:
@Echo Off
rd [dir_to_be_deleted] /S /Q

I then call this bat file from my C++ program using CreateProcess(). Once the bat file has finished, I remove it (the bat file) from the location I created it on using remove(path_and_file). Therefore the directory and bat file are removed from the user's hard drive.

I've tried the recursive route, but trust me, this is much simpler!