Click to See Complete Forum and Search --> : Use ListBox content to Delete files in directory and all subfolders


MaxDes
May 11th, 2009, 03:38 PM
VB.net 2008 express
This is what I have so far, and it loads a list of files from a text file. Now I need to be able to delete them all from a certain drive and all sub folders in it. For instance I push the delete button and it goes through the entire list and deletes all the files in it from the designated drive and all it's sub folders.
Any help is appreciated


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
Friend WithEvents deletebutton As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.Button2 = New System.Windows.Forms.Button
Me.deletebutton = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'ListBox2
'
Me.ListBox2.Location = New System.Drawing.Point(12, 12)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(296, 186)
Me.ListBox2.TabIndex = 4
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 204)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(110, 24)
Me.Button2.TabIndex = 5
Me.Button2.Text = "Load C:\Delete.txt"
'
'deletebutton
'
Me.deletebutton.Location = New System.Drawing.Point(13, 246)
Me.deletebutton.Name = "deletebutton"
Me.deletebutton.Size = New System.Drawing.Size(109, 23)
Me.deletebutton.TabIndex = 6
Me.deletebutton.Text = "Delete Files"
Me.deletebutton.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 293)
Me.Controls.Add(Me.deletebutton)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListBox2)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Dim w As IO.StreamWriter
Dim r As IO.StreamReader

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
r = New IO.StreamReader("c:\delete.txt")
While (r.Peek() > -1)
ListBox2.Items.Add(r.ReadLine)
End While
r.Close()
End Sub

Private Sub deletebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deletebutton.Click

End Sub
End Class

DataMiser
May 11th, 2009, 08:37 PM
System.IO.File.Delete(PathFileName)

HanneSThEGreaT
May 12th, 2009, 01:42 AM
You have to remember that you may need to delete the files in the subfolder first, before deleting the folder...

MaxDes
May 12th, 2009, 09:25 AM
I am only deleting files, not folders.

Also:

System.IO.File.Delete(PathFileName)

How do I use the information inside the listbox to make this work? - System.IO.File.Delete(PathFileName)

HanneSThEGreaT
May 12th, 2009, 10:09 AM
In your original message you said :
loads a list of files from a text file. Now I need to be able to delete them all from a certain drive and all sub folders in it.

Hence my reply :)

We don't know the contents of the C:\delete.txt file, so it's quite difficult to assist further without knowing it. Are there full file names in there ¿ Are there any other data in that file, besides file names ¿

MaxDes
May 12th, 2009, 11:50 AM
Yes that meant delete the files from the root dir and ALL subfolders in the root dir. Not the folders, the files themselves. Sorry I should have been more clear.

FVODmoviename.mpg

is the format of the list. It has and will usually have about 1600 entries. I'd like the program to go through and delete the movie files from the drive and also remove them from the list as it deletes them.

So it would need to look in

x:\

DataMiser
May 13th, 2009, 02:37 AM
Assuming that you have the path and filename in the list box you would simply loop through the items in the listbox and replace PathFileName with the item text in the list.

dglienna
May 13th, 2009, 07:25 PM
Doesn't sound like he has the path, unless they are all on the X: drive in the ROOT folder :sick:

MaxDes
May 15th, 2009, 04:16 PM
They would be in root and subfolders....

dglienna
May 15th, 2009, 06:33 PM
Your filename didn't have that. If your text file has that info, then you're set. Otherwise, add it.