Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# VB.Net Code to Delete all subfolders and files

Posted on 2011-09-27
Medium Priority
642 Views
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
Question by:ACECORP
1 Comment

LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 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

0

## Featured Post

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
###### Suggested Courses
Course of the Month6 days, 20 hours left to enroll