Solved

VB.Net Code to Delete all subfolders and files

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Syntax 9 36
Need help making a program or form for inventory that exports to txt 8 40
.NET 2008 VB and C# 6 27
VB.NET 2008 - SQL Timeout 9 24
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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