• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 68
  • Last Modified:

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

0
Jason Paradis
Asked:
Jason Paradis
1 Solution
 
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
 
Jason ParadisIT Operations SpecialistAuthor Commented:
This worked perfectly. Thank you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now