CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2008
    Posts
    7

    Use ListBox content to Delete files in directory and all subfolders

    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

    Code:
    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
    Last edited by MaxDes; May 11th, 2009 at 05:07 PM. Reason: more info

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Use ListBox content to Delete files in directory and all subfolders

    System.IO.File.Delete(PathFileName)

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Use ListBox content to Delete files in directory and all subfolders

    You have to remember that you may need to delete the files in the subfolder first, before deleting the folder...

  4. #4
    Join Date
    Dec 2008
    Posts
    7

    Re: Use ListBox content to Delete files in directory and all subfolders

    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)

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Use ListBox content to Delete files in directory and all subfolders

    In your original message you said :
    Quote Originally Posted by MaxDes
    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 &#191; Are there any other data in that file, besides file names &#191;

  6. #6
    Join Date
    Dec 2008
    Posts
    7

    Re: Use ListBox content to Delete files in directory and all subfolders

    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:\

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Use ListBox content to Delete files in directory and all subfolders

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use ListBox content to Delete files in directory and all subfolders

    Doesn't sound like he has the path, unless they are all on the X: drive in the ROOT folder
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Dec 2008
    Posts
    7

    Re: Use ListBox content to Delete files in directory and all subfolders

    They would be in root and subfolders....

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Use ListBox content to Delete files in directory and all subfolders

    Your filename didn't have that. If your text file has that info, then you're set. Otherwise, add it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured