Solved

VB.Net Code to Delete all subfolders and files

Posted on 2011-09-27
1
568 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36711032
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

16 Experts available now in Live!

Get 1:1 Help Now