Possible Duplicates:
How to delete a directory and its contents in (POSIX) C?
This is in a standard Linux environment.
Thanks!
(I'm aware of rmdir
but this isn't what I'm looking for.)
Possible Duplicates:
How to delete a directory and its contents in (POSIX) C?
This is in a standard Linux environment.
Thanks!
(I'm aware of rmdir
but this isn't what I'm looking for.)
You'll want to traverse the directory tree, using nftw for file tree walk. For the rm -r
example, use the flag FTW_DEPTH
to process contents first, and check for FTW_D
to use rmdir on directories rather than remove or unlink. Of course this doesn't guarantee you're allowed to remove things; that's generally decided by write persmission of the containing directory.
What's wrong with system("rm -rf");
?
update: since commenters are showing with their comments that it's easy to miss the point, let me expand to clarify:
if (chdir(dirName) != 0)
return -1;
if (system("rm -rf .") != 0)
return -2;