Delete subfolders and files in vb.net
Asked Answered
Y

1

6

Is it possible to delete all the subfolders (with content) and files within 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 can I delete the whole content (3 subfolders and 3 files) of the Backup folder without deleting the root folder (Backup) itself?

Yearbook answered 3/12, 2014 at 13:14 Comment(0)
N
14

The Directory class has a Delete method that accepts a parameter that forces the deleting operation recursively on the folder passed

' 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

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

Narcis answered 3/12, 2014 at 13:31 Comment(1)
Also, For Each DirectoryFile As String In Directory.GetFiles("c:\backup") File.Delete(DirectoryFile) Next to delete the files as well.Disinterest

© 2022 - 2024 — McMap. All rights reserved.