in reply to How do I delete a directory without emptying it first?
In theory, finddepth will find the deepest items first (files inside the directories). Once the files are unlinked, the directories will be empty and can be removed.use File::Find; finddepth (\&remove_dir, "$directory"); rmdir ( "$directory" ) or die ("Could not remove $directory"); sub remove_dir { # for a directory, this will be 0 if ( ! (stat("$File::Find::name"))[7] ) { rmdir("$File::Find::name"); } else { unlink("$File::Find::name"); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Answer: How do I delete a directory without emptying it first?
by gav^ (Curate) on Jan 17, 2002 at 07:11 UTC |