Search code examples
vb.netfilemergecopyxcopy

Need to merge 2 folders that have hundreds of thousands of files in each. VB.net


I need to merge these folders that have a TON of files in each directory. I would use folder.copy but that takes forever.

I have been using xcopy process to handle this for the most part but I have been getting errors from xcopy saying that there are duplicates when there are none. So I am trying to find a solid work around that is as fast or even faster then xcopy.

What I have tried before is:

Private Sub MergeF(ByVal TargetFolder As String, ByVal MergeeFolder As String)
    For Each F As String In IO.Directory.GetFiles(MergeeFolder)
        If IO.File.Exists(IO.Path.Combine(TargetFolder, IO.Path.GetFileName(F))) Then
            Dim FileA As New IO.FileInfo(IO.Path.Combine(
                MergeeFolder, IO.Path.GetFileName(F)))
            Dim FileB As New IO.FileInfo(IO.Path.Combine(
                TargetFolder, IO.Path.GetFileName(F)))
            If FileA.Length <> FileB.Length Then
                Dim index As Integer = 1
                Do
                    Dim NewFileName = IO.Path.Combine(TargetFolder,
                        IO.Path.GetFileName(F.Insert(F.Length - 4, CStr(index))))
                    If IO.File.Exists(NewFileName) Then
                        index += 1
                    Else
                        IO.File.Copy(F, NewFileName)
                        IO.File.Delete(F)
                        Exit Do
                    End If
                Loop
            End If
        Else
            IO.File.Move(IO.Path.Combine(MergeeFolder, IO.Path.GetFileName(F)),
                IO.Path.Combine(TargetFolder, IO.Path.GetFileName(F)))
        End If
    Next
End Sub

Solution

  • https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories

    async should speed things up also;

    https://learn.microsoft.com/en-us/dotnet/standard/io/asynchronous-file-i-o

    if that is not fast enough, then I would think your next option would be trying with straight win32api

    to convert code to vb you can use;

    https://codeconverter.icsharpcode.net/ or https://converter.telerik.com/

    • Goodluck!