Removing subfolders and files in vb.net

Is it possible to delete all subfolders (with content) and files in a folder?

For example:

  • Backup
    • November
      • pic1.jpg
      • pic2.jpg
    • December
    • January
      • pic3.jpg
    • example1.txt
    • example2.txt
    • example3.txt

There is a root folder (Backup). This root folder contains 3 subfolders (with content) and 3 text files. How to delete all content (3 subfolders and 3 files) from the "Backup" folder without deleting the root folder (Backup)?

+3


source to share


1 answer


The Directory class has a Delete method that takes a parameter that forces a delete operation on the passed folder

' Loop over the subdirectories and remove them with their contents
For Each d in Directory.GetDirectories("C:\Backup")
    Directory.Delete(d, true)
Next

' Finish removing also the files in the root folder
For Each f In Directory.GetFiles("c:\backup") 
     File.Delete(f) 
Next 

      



FROM MSDN Directory.Delete

Removes the specified directory and, if specified, any subdirectories and files in the directory.

+8


source







All Articles