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
1
Medium Priority
?
642 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 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

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

782 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