473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.IEnumerator
Collection = Directory.GetEn umerator
While Collection.Move Next
Try
'Sets file to read-only, to make it delete without
prompt/error
System.IO.File. SetAttributes(C ollection.Curre nt,
IO.FileAttribut es.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.Curr ent & 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 2865
Hi,

try using this method:

Call : DeleteFiles("C: \temp")

Public Sub DeleteFiles(ByV al sPath As String)

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

f = Directory.GetFi les(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.FileAttribut es.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.GetDi rectories(sPath )
For i As Integer = 0 To d.GetUpperBound (0)
DeleteFiles(CTy pe(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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
sDirectory = "C:\Test"
DeleteFilesAndF olders(sDirecto ry)
End Sub

Public Sub DeleteFilesAndF olders(ByVal sPath As String)
Dim f() As String
Dim d() As String

f = Directory.GetFi les(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.FileAttribut es.Normal)
File.Delete(f(i ))
Catch
'Set trigger that a file failed to delete

End Try
Next
End If

d = Directory.GetDi rectories(sPath )
For i As Integer = 0 To d.GetUpperBound (0)
DeleteFilesAndF olders(CType(d( i), String))
Next

If sDirectory <sPath Then
Try
Directory.Delet e(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(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
sDirectory = "C:\Test"
DeleteFilesAndF olders(sDirecto ry)
End Sub

Public Sub DeleteFilesAndF olders(ByVal sPath As String)
Dim f() As String
Dim d() As String

f = Directory.GetFi les(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.FileAttribut es.Normal)
File.Delete(f(i ))
Catch
'Set trigger that a file failed to delete

End Try
Next
End If

d = Directory.GetDi rectories(sPath )
For i As Integer = 0 To d.GetUpperBound (0)
DeleteFilesAndF olders(CType(d( i), String))
Next

If sDirectory <sPath Then
Try
Directory.Delet e(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
582
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, Laura
5
2198
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 = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set oFolderContents = oFolder.Files
5
16763
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 and no files are deleted in the folder. The peculiarity of this issue is that the files/subfolders cannot be seen through the windows explorer...
5
4013
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 load the data by something like that: LOAD FROM "/pathto/artikel.data" OF DEL MODIFIED BY DATESISO CHARDEL0xbf SAVECOUNT 50000 MESSAGES...
3
1512
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 doesnt exist AppPath = Request.PhysicalApplicationPath + UploadDirectory; if(!Directory.Exists(AppPath))...
3
2280
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 "D:\". at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path) at...
6
4546
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 restarts and all active sessions are terminated. This error is also described in detail here:...
4
1638
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 using rmdir command to remove the source directory.. the problem which i am facing is.. For example, i have two files in the directory named A and the...
7
5474
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 enumerate through that array to get my listing. However I am trying to set my application up as a service. And now (after calling methods in a...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.