VB - CopyDirectory not copying Documents folder due to Junction Folders

I have an app that's supposed to copy specific folders within the currently logged in User profile. I have already added a Try Catch to bypass the Junction folders in Documents but it seems to be bypassing the entire directory completely. I need it to copy the Documents folder contents while ignoring any Junction Folder errors.

The first module is just for calculating size and can be ignored, it's for a different button click.

Thank you.

Imports System
Imports System.IO
Imports System.Environment

Module Module1
    Sub GetSize()
        Dim TotalSize As Long
        Dim DocSize As Long = String.Format("{0}\Documents\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim DowSize As Long = String.Format("{0}\Downloads\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim ConSize As Long = String.Format("{0}\Contacts\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim DesSize As Long = String.Format("{0}\Desktop\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim FavSize As Long = String.Format("{0}\Favorites\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim PicSize As Long = String.Format("{0}\Pictures\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        Dim VidSize As Long = String.Format("{0}\Videos\", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).GetDirectorySize()
        TotalSize = (DocSize + DowSize + ConSize + DesSize + FavSize + PicSize + VidSize)
        MsgBox("The total backup size is:" & vbCr &
               FormatNumber(TotalSize, 0) & " Bytes" & vbCr &
               FormatNumber(TotalSize / 1024, 1) & " Kilobytes" & vbCr &
               FormatNumber(TotalSize / 1024 / 1024, 1) & " Megabytes" & vbCr &
               FormatNumber(TotalSize / 1024 / 1024 / 1024, 1) & " Gigabytes")
    End Sub
End Module

Module Module2
    Sub CopyFunction()
        Dim pbox = New PleaseWaitForm()
        pbox.Show()
        Dim folders = New String() {"Documents", "Downloads", "Pictures", "Contacts", "Desktop", "Favorites", "Videos"}
        Dim sourcePath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
        Dim targetPath As String = String.Format("\\destinationserver\users\{0}", Environment.UserName)
        Dim size As Long = Path.Combine(sourcePath, folders(0)).GetDirectorySize()
        Console.WriteLine("{1} Bytes{0}{2} Kilobytes{0}{3} Megabytes{0}{4} Gigabytes", vbCrLf, FormatNumber(size, 0), FormatNumber(size / 1024, 1), FormatNumber(size / 1024 / 1024, 1), FormatNumber(size / 1024 / 1024 / 1024, 1))
        For Each folder In folders
            If Path.Combine(sourcePath, folder).CopyDirectory(Path.Combine(targetPath, folder), True) Then
                Console.WriteLine("Successfully copied; {0} to; {1}", Path.Combine(sourcePath, folder), Path.Combine(targetPath, folder))
            Else
                Console.WriteLine("Copy failed for; {0} to; {1}", Path.Combine(sourcePath, folder), Path.Combine(targetPath, folder))
            End If
        Next
        Console.ReadLine()
        pbox.Close()
    End Sub
End Module

Module Extensions
    <System.Runtime.CompilerServices.Extension>
    Function GetDirectorySize(folder As String) As Long
        Return New DirectoryInfo(folder).GetDirectorySize().Sum()
    End Function

    <System.Runtime.CompilerServices.Extension>
    Iterator Function GetDirectorySize(folder As DirectoryInfo) As IEnumerable(Of Long)
        For Each [file] In folder.EnumerateFiles
            Try
                Yield [file].Length
            Catch ex As Exception
            End Try
        Next
        For Each [child] In folder.EnumerateDirectories
            Try
                Yield [child].GetDirectorySize().Sum()
            Catch ex As Exception
            End Try
        Next
    End Function

    <System.Runtime.CompilerServices.Extension>
    Function CopyDirectory(source As String, target As String, overwrite As Boolean) As Boolean
        Dim result As Boolean = False
        Try
            My.Computer.FileSystem.CopyDirectory(source, target, overwrite)
            result = True
        Catch ex As Exception
            result = False
        End Try
        Return result
    End Function
End Module

Open in new window

LVL 1
Jason ParadisIT Operations SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You will need to copy the files instead of using the CopyDirectory Method; e.g. -
Module Extensions
    <Extension>
    Function CopyDirectory(source As String, target As String, overwrite As Boolean) As Boolean
        Dim result As Boolean = False
        Dim srcDirectory As DirectoryInfo = Nothing
        Dim tgtDirectory As DirectoryInfo = Nothing
        Try
            srcDirectory = New DirectoryInfo(source)
            tgtDirectory = New DirectoryInfo(target)

            If Not tgtDirectory.Exists Then tgtDirectory.Create()

            If srcDirectory.Exists Then
                For Each file In srcDirectory.EnumerateFiles()
                    CopyFile(file.Name, srcDirectory.FullName, tgtDirectory.FullName, overwrite)
                Next

                For Each directory In srcDirectory.EnumerateDirectories()
                    CopyDirectory(directory.FullName, Path.Combine(target, directory.Name), overwrite)
                Next
                result = True
            End If
        Catch ex As Exception
            result = False
        End Try
        Return result
    End Function

    <Extension>
    Function CopyFile(file As String, source As String, target As String, overwrite As Boolean) As Boolean
        Dim results As Boolean = False
        Dim srcFile As FileInfo = Nothing
        Dim tgtFile As FileInfo = Nothing
        Try
            srcFile = New FileInfo(Path.Combine(source, file))
            tgtFile = New FileInfo(Path.Combine(target, file))

            If Not tgtFile.Directory.Exists Then tgtFile.Directory.Create()

            If srcFile.Exists Then
                srcFile.CopyTo(tgtFile.FullName, overwrite)
                tgtFile = New FileInfo(tgtFile.FullName)
                results = tgtFile.Exists
            End If
        Catch ex As Exception
            '' If we get an exception, the copy most likely failed
            results = False
        End Try
        Return results
    End Function
End Module

Open in new window


You should be able to just use the same call you do now.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason ParadisIT Operations SpecialistAuthor Commented:
This worked perfectly. Thank you very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.