Deleting directory path and files. VB.net 2003


K

Kimera.Kimera

I'm trying to write a program in VB.net 2003 that basically deletes all
files, folders, sub-folders and sub-sub folders (etc).

The program is simply for deleting the Windows/Temp folder contents,
removing all the files/folders inside it.

The problem i am having is that i can only delete files in the
Windows/Temp folder, and i can't delete folders if they contain files,
i know that you can't do this.

But i don't know how i would go about deleting the files inside
folders/sub folders (and even deeper into a path)

I have looked and looked all over the place and i've not been able to
find a solution.

Here is my code so far for deleting files:

Imports:
Imports System.IO

Variables:
'Used to control Directory for deleting files
Public Directory As String

Dim Collection As System.Collections.IEnumerator
Collection = Directory.GetEnumerator
While Collection.MoveNext
Try
'Sets file to read-only, to make it delete without
prompt/error
System.IO.File.SetAttributes(Collection.Current,
IO.FileAttributes.Normal)
'Reads in sub dir and kills their files
Kill(Collection.Current)
Catch
'Set trigger that a file failed to delete
DeletionFail = True
'Add erroneous files to list variable, later to be
displayed
NotDeleted &= ("Unable to delete file : " &
Collection.Current & vbCrLf)
End Try
End While


Any other solution is welcome that will simply delete all
files/folders/sub dir's inside a spesfied directory (IE,
C:\WINDOWS\Temp)
 
Ad

Advertisements

K

krishnen

Hi,

try using this method:

Call : DeleteFiles("C:\temp")

Public Sub DeleteFiles(ByVal sPath As String)

Dim f() As String
Dim d() As String

f = Directory.GetFiles(sPath)

If f.GetLength(0) > 0 Then
For i As Integer = 0 To f.GetUpperBound(0)
Try
'Sets file to read-only, to make it delete without
System.IO.File.SetAttributes(f(i),
IO.FileAttributes.Normal)
File.Delete(f(i))
Catch
'Set trigger that a file failed to delete
'rest of your code here
End Try
Next
End If

d = Directory.GetDirectories(sPath)
For i As Integer = 0 To d.GetUpperBound(0)
DeleteFiles(CType(d(i), String))
Next

End Sub

Krishnen
 
K

krishnen

I think this one may suit you better as you need to delete the folders
as well:

'Used to control Directory for deleting files
Public sDirectory As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
sDirectory = "C:\Test"
DeleteFilesAndFolders(sDirectory)
End Sub

Public Sub DeleteFilesAndFolders(ByVal sPath As String)
Dim f() As String
Dim d() As String

f = Directory.GetFiles(sPath)

If f.GetLength(0) > 0 Then
For i As Integer = 0 To f.GetUpperBound(0)
Try
'Sets file to read-only, to make it delete without
System.IO.File.SetAttributes(f(i),
IO.FileAttributes.Normal)
File.Delete(f(i))
Catch
'Set trigger that a file failed to delete

End Try
Next
End If

d = Directory.GetDirectories(sPath)
For i As Integer = 0 To d.GetUpperBound(0)
DeleteFilesAndFolders(CType(d(i), String))
Next

If sDirectory <> sPath Then
Try
Directory.Delete(sPath)
Catch ex As Exception

End Try
End If

End Sub
 
Ad

Advertisements

K

Kimera.Kimera

krishnen said:
I think this one may suit you better as you need to delete the folders
as well:

'Used to control Directory for deleting files
Public sDirectory As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
sDirectory = "C:\Test"
DeleteFilesAndFolders(sDirectory)
End Sub

Public Sub DeleteFilesAndFolders(ByVal sPath As String)
Dim f() As String
Dim d() As String

f = Directory.GetFiles(sPath)

If f.GetLength(0) > 0 Then
For i As Integer = 0 To f.GetUpperBound(0)
Try
'Sets file to read-only, to make it delete without
System.IO.File.SetAttributes(f(i),
IO.FileAttributes.Normal)
File.Delete(f(i))
Catch
'Set trigger that a file failed to delete

End Try
Next
End If

d = Directory.GetDirectories(sPath)
For i As Integer = 0 To d.GetUpperBound(0)
DeleteFilesAndFolders(CType(d(i), String))
Next

If sDirectory <> sPath Then
Try
Directory.Delete(sPath)
Catch ex As Exception

End Try
End If

End Sub


Thank you so much for spending your time helping me :D

I've just tried it and it works a treat so, i'm just going to go
through the code and work out just how it works, Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top