By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Deleting directory path and files. VB.net 2003

P: n/a
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)

Dec 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Dec 29 '06 #2

P: n/a
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

Dec 29 '06 #3

P: n/a

krishnen wrote:
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.

Dec 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.