Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I delete all files and sub folders in a folder?

Directories / Folders

How do I delete all files and sub folders in a folder?

by  KevinADC  Posted    (Edited  )
----------------------------
[small]ignore this section:
delete files
deleting files[/small]
----------------------------

Problem :

You want to delete all the files and sub folders (and all their files and sub folders) in a folder. But to delete files you need to use the unlink() function and for folders the rmdir() function, and besides, you don't know how to recursively search through all the sub folders to delete all the files first so you can delete all the folders.

Solution :

Use the [link http://perldoc.perl.org/File/Path.html]File::path[/link] (core) module.

Discussion :

The module is so easy to use that there is really not much to discuss. Click on the link above and read the documentation. You can also use the module to create directory trees.

[red]Important Note[/red]

The only thing to keep in mind is that deleting files and folders is most likely going to be a permanent situation. So make sure you really want to delete an entire directory tree and make sure you are starting in the correct folder before invoking the script. If you make a mistake and start in the wrong directory you could suffer permanent loss of all the data.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top