Solved

VB.Net Code to Delete all subfolders and files

Posted on 2011-09-27
1
559 Views
Last Modified: 2012-05-12
I have a folder that contains a lot of files, subfolders and files in the subfolders.

I do not want to delete the parent/top level folder, i just want to delete all the files,  subfolders, and files each subfolder contains.

All the code I have seen on the internet and EE allows you to specify the path/parent/top level folder, but it deletes the parent/top level folder that you specify, in addition to all subfolders and files.

That wont work for me, I need to specify a parent folder, but only the files, subfolders, and files in the subfolders get deleted, not the parent folder itself.

Could someone provide me with the VB.Net code that will allow me to delete just the files, subfolders, and files in the subfolders while leaving the top level/parent folder intact?.

0
Comment
Question by:ACECORP
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
It should be as simple as:
Dim parent As New System.IO.DirectoryInfo("C:\path\to\parent")

For Each file As System.IO.FileInfo In parent.GetFiles()
    file.Delete()
Next

For Each folder As System.IO.DirectoryInfo In parent.GetDirectories()
    folder.Delete(True)
Next

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now