473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Deleting directory path and files. VB.net 2003

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
3 2856
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Laura McCord | last post by:
Hi, I need to delete all directories in /var/www/html/test that are older than five days. I am new to python so how would I get started? Any tips or suggestions would be appreciated. Thanks, ...
5
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO =...
5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
5
by: Olaf Gschweng | last post by:
We're new into DB2 and have some problem with DB2 8.1 (?) on a Linux system. We load some big tables of a DB2 database from files every day. We do a "DELETE FROM table" for each table and then we...
3
by: troy | last post by:
Hello, I have the following code that run in the page_load section: private void Page_Load(object sender, System.EventArgs e) { /// Create uploads folder in the Virtua; directory if one...
3
by: James Coleman | last post by:
Hello, The following error is appearing when attempting to create a directory using the availale system.io methods: System.IO.DirectoryNotFoundException: Could not find a part of the path...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
4
by: RKalai | last post by:
Hi I am moving list of files from one directory to another directory.i am doing it by first moving all the files from the source directory to destination directory using filecopy command.then...
7
by: epikto | last post by:
I have a mapped share that I am trying to get a listing of all the files that it contains. I use the following code to access the contents String files = Directory.GetFiles(path); I can then...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.