Deleting the contents of a Directory

SonicBoomAu

Well-known member
Joined
Oct 30, 2003
Messages
179
Location
Australia
Does anyone know how to delete the contents of a directory without deleting the root directory and with out know the folder / file names?

I havent been able to get FSO to work or the isolatedStorageFile.

Help please.
 
You can have two methods, choos the one you like...

1st Method
first get all the sub directories
Dim ArrDirectories() As String = Directory.GetDirectories("The Path Of Your Directory")


Dim i As Integer

loop through the directories and delete them including their sub items
For i = 0 To ArrDirectories.Length - 1
specifying a true would mean that you want to delete everything inside
Directory.Delete(ArrDirectories(i), True)
Next

then get the list of files in the directory
Dim ArrFiles() As String = Directory.GetDirectories("The Path Of Your Directory")

loop through the files and delete them
For i = 0 To ArrFiles.Length - 1
File.Delete(ArrFiles(i))
Next

now you have an empty directory...

2nd Method
Another alternative is that delete the whole directory with its sub folders and then create a new
one with the same name instead.
Dim Path As String = "The Path Of Your Directory"

delete the directory with everyhting inside it
Directory.Delete(Path, True)

Create a new one in its place
Directory.CreateDirectory(Path)


Hope this helps,
 
Thanks for that.
I used the 1st Method. I couldnt delete the root folder due to shares and permissions that i would have to reset.

The only thing I had to change was with:
Dim ArrFiles() As String = Directory.GetDirectories("The Path Of Your Directory")

I changed Directory.GetDirectories to Directory.GetFiles

Once again, Thanks for that.
 
Back
Top