Copying files and moving files

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
This is pretty simple. I'm just testing things and seeing if I can get things working and if I can then I want to try and develop this into something more. So far its just a simple button1 finds all files with extension of .mp3 in C:\ and displays all the findings in a listbox. I then want to use button2 to copy or move (I want to see how to do both) all of the files that are in that lisbox to a directory of my choosing. Here is what I have so far.


VB.NET:
Imports System.IO

Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    searchdrive("c:\")
  End Sub

  Private Sub searchdrive(ByVal Path As String)
    Try
      Dim CurDir As DirectoryInfo = New DirectoryInfo(Path)
      Dim DirArray() As DirectoryInfo = CurDir.GetDirectories
      Dim fileInf As FileInfo
      Dim dirinfo As DirectoryInfo
      Dim fileArray() As FileInfo = CurDir.GetFiles
      For Each dirinfo In DirArray
        searchdrive(dirinfo.FullName)
      Next
      For Each fileInf In fileArray
        If LCase(fileInf.Extension) = ".mp3" Then
          ListBox1.Items.Add(fileInf.Name) '---adds Filename only to a listbox 
        End If
        Application.DoEvents()
      Next
    Catch
    End Try
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    CopyFiles()

  End Sub

  Public Sub CopyFiles()


  End Sub
End Class
 
Actually I'm going about this wrong.

Basically what I want to do is search my entire computer for MP3 files then have the option to move them all to a specific folder on a drive of my choosing.

Eventually do the same thing with other files but I think if I learn this first part I can do the rest. I hate asking for too much help but I been trying on my own all day and finally broke down and asked.
 
A few suggestions, I would make a class to store the full file path and use it's ToString override to display just the file name in the ListBox.

I would also do all hard drive searching and file copying/moving into a backgound thread, the BackgroundWorker is really nice for this purpose.

When searching the hard drive for file types, you'll want to do recursion, as in have a sub that accepts a string (root folder path) and for each folder in it, call the same sub passing it the sub folder path. If you need an example, let me know.
 
I'd link a BindingList(Of FileInfo) and just set DisplayMember to "Name". Basically this setup:
VB.NET:
Private list As New System.ComponentModel.BindingList(Of IO.FileInfo)
bind for example in forms Load event:
VB.NET:
Me.ListBox1.DataSource = list
Me.ListBox1.DisplayMember = "Name"
example adding files to list:
VB.NET:
Dim d As New IO.DirectoryInfo("c:\")
For Each file In d.GetFiles("*.mp3")
    list.Add(file)
    Application.DoEvents()
Next
later moving selected files:
VB.NET:
For Each file As IO.FileInfo In Me.ListBox1.SelectedItems
    file.MoveTo("dest file name")
Next
-------------
But I would do away with DoEvents and do multithreading instead. Using the BackgroundWorker component is really simple to use. Add one to the form from Toolbox (Components), set WorkerReportsProgress to True. Doubleclick it to get the DoWork event handler, add the ProgressChanged event handler also. Now in Button click handler you call this to start the background work:
VB.NET:
BackgroundWorker1.RunWorkerAsync()
In DoWork handler call the method:
VB.NET:
searchdrive("c:\")
In the search loop instead of adding to list directly report it as progress (otherwise you get cross-threading issues):
VB.NET:
Me.BackgroundWorker1.ReportProgress(0, f)
and in ProgressChanged handler add it to the list:
VB.NET:
list.Add(CType(e.UserState, IO.FileInfo))
Now you can also have a button to cancel the search, just set WorkerSupportsCancellation to True and call CancelAsync to request cancellation, in the worker loop check for CancellationPending.
 
Hi John, thanks for helping. I'm trying to add your suggestions but I'll admit it I'm not that great at this and can't seem to add things right or get things working. Below is where I stopped. Make you can edit and re-post what I have here? Its unfortunate, I have a Bachelors in Software Development but the actual programming classes were far and few between.

VB.NET:
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
        searchMusic("C:\")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For Each file As IO.FileInfo In Me.ListBox1.SelectedItems
            file.MoveTo("C:\Test\")
        Next
    End Sub
    Private list As New System.ComponentModel.BindingList(Of IO.FileInfo)
    Private Sub searchMusic(ByVal Path As String)
        Try
            Dim CurDir As DirectoryInfo = New DirectoryInfo(Path)
            Dim DirArray() As DirectoryInfo = CurDir.GetDirectories
            Dim fileArray() As FileInfo = CurDir.GetFiles
            Dim d As New IO.DirectoryInfo(Path)
            For Each file In d.GetFiles("*.mp3")
                list.Add(file)
                Application.DoEvents()
            Next
        Catch
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DataSource = list
        Me.ListBox1.DisplayMember = "Name"
    End Sub
End Class
 
You need event handlers for the BackgroundWorker event DoWork and ProgressChanged. Adding event handlers is exactly the same as for any other form/control/component.
  • To run work async call RunWorkerAsync, this is what you do from button click.
  • To do work async add code to the DoWork event handler, this is where you call searchMusic.
 
John I'm sure I'm making your head hurt by now.

VB.NET:
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
        searchMusic("C:\")
        Call RunWorkerAsync("C:\")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For Each file As IO.FileInfo In Me.ListBox1.SelectedItems
            file.MoveTo("C:\Test\")
        Next
    End Sub
    Private list As New System.ComponentModel.BindingList(Of IO.FileInfo)
    Private Sub searchMusic(ByVal Path As String)
        Try
            Dim CurDir As DirectoryInfo = New DirectoryInfo(Path)
            Dim DirArray() As DirectoryInfo = CurDir.GetDirectories
            Dim fileArray() As FileInfo = CurDir.GetFiles
            Dim d As New IO.DirectoryInfo(Path)
            For Each file In d.GetFiles("*.mp3")
                list.Add(file)
                searchMusic(Path)
                Application.DoEvents()
            Next
        Catch
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DataSource = list
        Me.ListBox1.DisplayMember = "Name"
    End Sub

    Private Sub RunWorkerAsync(ByVal path)
        Dim d As New IO.DirectoryInfo(Path)
        For Each file In d.GetFiles("*.mp3")
            list.Add(file)
            searchMusic(Path)
            Application.DoEvents()
        Next
    End Sub

End Class
 
How did you get handlers for Form1 Load event and Button1/Button2 Click event?
 
Back
Top