Link to home
Start Free TrialLog in
Avatar of Jason Paradis
Jason ParadisFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason Paradis

ASKER

This worked perfectly. Thank you very much!