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 ) 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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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...
|
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...
|
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))...
| |
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...
|
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:...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |