Copying new files

KoyoteGambit

New member
Joined
May 14, 2008
Messages
4
Location
Jamaica
Programming Experience
3-5
I thought I had it, but I'm past frustration and I need some help. I have two directories LOCAL and REMOTE . I want to copy all the files that are in the REMOTE that ARE NOT in the LOCAL to the LOCAL. I have two list boxes that showed the content of both Directories and a copy button, everything works except it is to only copy what is missing from the LOCAL folder, instead its copying all the files. My code is below, what exactly and I doing wrong please?
VB.NET:
Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click
    'Checks the Remote Dir to see if files are in the Local Dir
    For k As Integer = ListBoxRemote.Items.Count - 1 To 0 Step -1
        For g As Integer = ListBoxLocal.Items.Count - 1 To 0
            If ListBoxLocal.Items(g).text = ListBoxRemote.Items(k).text Then
                MessageBox.Show("No New Files Founds")
            End If
        Next g
        MessageBox.Show("New File Found. Copying to LOCAL location")
        Dim fi As New FileInfo(sRemoteDirName & ListBoxRemote.Items(k))
        Dim overwrite As Boolean = True
        fi.CopyTo(sLocalDirName & ListBoxRemote.Items(k), overwrite)
    Next k
End Sub
 
What's your logic here?

Try writing comments in your code that explain what it is doing, and you'll find your error. Single step it in the debugger and you'll see your error. Even pro programmers like me do this; i did it this morning to find an obscure logic bug in an SQL. To err is human, to comment divine. ;)
 
For one thing:
VB.NET:
        Dim overwrite As Boolean = True
        fi.CopyTo(sLocalDirName & ListBoxRemote.Items(k), overwrite)

You set the overwrite boolean to True thus the files that already exist will be overwritten, doesn't that defeat what you're trying to do. Set the overwrite boolean to False and see what happens (the existing files should be left alone)

Another way to do it is to create a new List(Of String) that contains only the files that are not in the Local directory, then copy the files on that list, but this is more work than you need and would mean more code for you to maintain in the long run.
 
What do you mean by "didn't work"? Did it still overwrite the files, or did it throw an exception?

Anyhow, there's a much easier way of doing this :
VB.NET:
Imports System.IO
...
Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click
        Dim dir1 As New DirectoryInfo("c:\remote")
        Dim dir2 As New DirectoryInfo("c:\local")
        For Each File1 As FileInfo In dir1.GetFiles
            If File.Exists(dir2.FullName & "\" & File1.Name) Then
            Else
                File.Copy(File1.FullName, dir2.FullName)
            End If
        Next
End Sub

This may need a little editing (not sure on exact format .FullName and .Name returns off the top of my head), but it'll get the job done.
 
delstar, I advise the following in order to build directory paths:
VB.NET:
Path.Combine(dir2.FullName, File1.Name)


I did try without the overwrite and it didnt work either.

Er, that's because the logic is broken, tbh.. Nothing to do with whether you overwrite files or not, but the simplest logic to sync a source with a dest is:

For each file in source
if not exists file in dest then copy to dest


Now, just write code that implements that logic!
 
VB.NET:
Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click
        ' Start the loop with the last item in ListBoxRemote, counting down
        ' with each iteration
        For k As Integer = ListBoxRemote.Items.Count - 1 To 0 Step -1
            ' Start an inner loop with the last item in ListBoxLocal, * here you
            ' are missing the Step keyword and decrement value, which means
            ' ONLY the last item within ListBoxLocal will ever be compared
            ' against every item within ListBoxRemote in the outer loop.
            ' Following your logic here, if ever the Text value of last item within
            ' ListBoxLocal matches with the Text value of the current iterated
            ' item in ListBoxRemote then the code should display a prompt
            ' saying "No New Files Found", after which the program flow moves
            ' on to the next line out of this inner loop
            ' Practically, this inner loop is irrelevant because it doesn't have
            ' codes to prevent you from executing the fi.CopyTo() function 
            ' after exiting the loop
            For g As Integer = ListBoxLocal.Items.Count - 1 To 0
                If ListBoxLocal.Items(g).text = ListBoxRemote.Items(k).text Then
                    MessageBox.Show("No New Files Founds")
                End If
            Next g
            ' The remaining code below will ALWAYS be executed the same
            ' number of times as the number of items you have in
            ' ListBoxRemote, for example if you have 30 items in ListBoxRemote,
            ' this sub will copy ALL 30 files from ListBoxRemote to ListBoxLocal
            ' because your outer loop tells the computer to iterate 30 rounds 
            ' and there is no specific conditions to skip the fi.CopyTo() function 
            MessageBox.Show("New File Found. Copying to LOCAL location")
            Dim fi As New FileInfo(sRemoteDirName & ListBoxRemote.Items(k))
            Dim overwrite As Boolean = True
            fi.CopyTo(sLocalDirName & ListBoxRemote.Items(k), overwrite)
        Next k
    End Sub

hope my comments help you see the program flow clearer...
 
Last edited:
Back
Top