|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Deleting directory path and files. VB.net 2003files, 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) 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 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 krishnen wrote:
Show quoteHide quote > I think this one may suit you better as you need to delete the folders Thank you so much for spending your time helping me :D> 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 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.
How do I sort an ArrayList of objects by a property value
working with Access in VB.Net Binary Search Tree - CompareTo Error Waiting for a process to halt before continuing Trying to get started with tables... How do I handle a NULL XmlElement? Get spawned process how to check if the dataset is EOF or if a record exists frist . Change Connection String during runtime GotFocus versus PreviewKeyDown or Other |
|||||||||||||||||||||||