Copy all files and subfolders in folder

Posted on 2005-04-28
Last Modified: 2008-08-13
I currently have a sub that copies a specific file from one directory to multiple remote servers - what I am looking for is a similar process by which I can grab all the files of a specific folder (including sub folders) and copy them to the remote servers - original single file copy code below:
    Sub FileCopy2()

        Dim sSorurce As String = txtFileSource.Value
        Dim sFolder As String = "\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Support_GP.dsn"
        Dim sDestinations() As String = Array.CreateInstance(GetType(String), 46)

        sDestinations(0) = "\\LINKSERVER-6030" & sFolder
        sDestinations(1) = "\\LINKSERVER-6050" & sFolder
        sDestinations(2) = "\\LINKSERVER-6070" & sFolder
        sDestinations(3) = "\\LINKSERVER-6090" & sFolder
        sDestinations(4) = "\\LINKSERVER-6100" & sFolder
        sDestinations(5) = "\\LINKSERVER-6150" & sFolder
        sDestinations(6) = "\\LINKSERVER-6200" & sFolder
        sDestinations(7) = "\\LINKSERVER-6220" & sFolder
        sDestinations(8) = "\\LINKSERVER-6230" & sFolder
        sDestinations(9) = "\\LINKSERVER-6270" & sFolder
        sDestinations(10) = "\\LINKSERVER-6310" & sFolder
        sDestinations(11) = "\\LINKSERVER-6370" & sFolder
        sDestinations(12) = "\\LINKSERVER-6410" & sFolder
        sDestinations(13) = "\\LINKSERVER-6440" & sFolder
        sDestinations(14) = "\\LINKSERVER-6450" & sFolder
        sDestinations(15) = "\\LINKSERVER-6470" & sFolder
        sDestinations(16) = "\\LINKSERVER-6480" & sFolder
        sDestinations(17) = "\\LINKSERVER-6490" & sFolder
        sDestinations(18) = "\\LINKSERVER-6500" & sFolder
        sDestinations(19) = "\\LINKSERVER-6520" & sFolder
        sDestinations(20) = "\\LINKSERVER-6550" & sFolder
        sDestinations(21) = "\\LINKSERVER-6570" & sFolder
        sDestinations(22) = "\\LINKSERVER-6580" & sFolder
        sDestinations(23) = "\\LINKSERVER-6590" & sFolder
        sDestinations(24) = "\\LINKSERVER-6610" & sFolder
        sDestinations(25) = "\\LINKSERVER-6620" & sFolder
        sDestinations(26) = "\\LINKSERVER-6630" & sFolder
        sDestinations(27) = "\\LINKSERVER-6640" & sFolder
        sDestinations(28) = "\\LINKSERVER-6650" & sFolder
        sDestinations(29) = "\\LINKSERVER-6670" & sFolder
        sDestinations(30) = "\\LINKSERVER-6680" & sFolder
        sDestinations(31) = "\\LINKSERVER-6690" & sFolder
        sDestinations(32) = "\\LINKSERVER-6700" & sFolder
        sDestinations(33) = "\\LINKSERVER-6710" & sFolder
        sDestinations(34) = "\\LINKSERVER-6720" & sFolder
        sDestinations(35) = "\\LINKSERVER-6740" & sFolder
        sDestinations(36) = "\\LINKSERVER-6750" & sFolder
        sDestinations(37) = "\\LINKSERVER-6760" & sFolder
        sDestinations(38) = "\\LINKSERVER-6770" & sFolder
        sDestinations(39) = "\\LINKSERVER-7030" & sFolder
        sDestinations(40) = "\\LINKSERVER-7050" & sFolder
        sDestinations(41) = "\\LINKSERVER-7170" & sFolder
        sDestinations(42) = "\\LINKSERVER-7180" & sFolder
        sDestinations(43) = "\\LINKSERVER-7190" & sFolder
        sDestinations(44) = "\\LINKSERVER-7230" & sFolder
        sDestinations(45) = "\\LINKSERVER-7300" & sFolder

        Dim sDestination As String
        Dim ErrorMessage As String
        For Each sDestination In sDestinations
                File.Copy(sSorurce, sDestination, True)
            Catch e As Exception
                ErrorMessage += e.Message + vbCrLf
            End Try
            'If ErrorMessage.Length > 0 Then MsgBox(ErrorMessage)

    End Sub
Question by:tbaseflug
    LVL 7

    Expert Comment

       Private Function FindFiles(ByVal rootPath As String) As ArrayList
            Dim al As ArrayList = New ArrayList

            'Search current directory.
            For Each f As String In System.IO.Directory.GetFiles(rootPath)

            Dim alTemp As ArrayList = FindFilesR(rootPath)
            For Each fTemp As String In alTemp

            Return al
        End Function

        Private Function FindFilesR(ByVal path As String) As ArrayList
            Dim al As ArrayList = New ArrayList

            'Search all sub directories.
            For Each dir As String In System.IO.Directory.GetDirectories(path)
                For Each f As String In System.IO.Directory.GetFiles(dir)
                For Each fTemp As String In FindFilesR(dir)

            Return al
        End Function

    Call FindFiles function with the root pay and you will get back an array list of all the files in the root directory and then all subdirectories recursivley.


    Author Comment

    Eric -

    Thanks - I am an idiot - can you show me how I would plug something like this into my existing script?  Thanks!!!!!

    Author Comment

    Is it possible to just copy a directory, and by doing so, copy over all sub directories and files included within?
    LVL 10

    Accepted Solution

    Heres what I use

        Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, Optional ByVal Overwrite As Boolean = False)
            Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath)
            Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath)

            ' the source directory must exist, otherwise throw an exception
            If SourceDir.Exists Then
                ' if destination SubDir's parent SubDir does not exist throw an exception
                If Not DestDir.Parent.Exists Then
                    Throw New DirectoryNotFoundException("Destination directory does not exist: " + DestDir.Parent.FullName)
                End If

                If Not DestDir.Exists Then
                End If

                ' copy all the files of the current directory
                Dim ChildFile As FileInfo
                For Each ChildFile In SourceDir.GetFiles()
                    If Overwrite Then
                        ChildFile.CopyTo(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name), True)
                        ' if Overwrite = false, copy the file only if it does not exist
                        ' this is done to avoid an IOException if a file already exists
                        ' this way the other files can be copied anyway...
                        If Not File.Exists(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name)) Then
                            ChildFile.CopyTo(System.IO.Path.Combine(DestDir.FullName, ChildFile.Name), False)
                        End If
                    End If

                ' copy all the sub-directories by recursively calling this same routine
                Dim SubDir As DirectoryInfo
                For Each SubDir In SourceDir.GetDirectories()
                    CopyDirectory(SubDir.FullName, System.IO.Path.Combine(DestDir.FullName, SubDir.Name), Overwrite)
                Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
            End If
        End Sub


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now