• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

Copy all files and subfolders in folder

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
            Try
                File.Copy(sSorurce, sDestination, True)
            Catch e As Exception
                ErrorMessage += e.Message + vbCrLf
            End Try
            'If ErrorMessage.Length > 0 Then MsgBox(ErrorMessage)
        Next

    End Sub
'----------------------------------------------------------------------------------------------
0
tbaseflug
Asked:
tbaseflug
  • 2
1 Solution
 
RealMrTeaCommented:
   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)
            al.Add(f)
        Next

        Dim alTemp As ArrayList = FindFilesR(rootPath)
        For Each fTemp As String In alTemp
            al.Add(fTemp)
        Next

        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)
                al.Add(f)
            Next
            For Each fTemp As String In FindFilesR(dir)
                al.Add(fTemp)
            Next
        Next

        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.

Later,
Eric
0
 
tbaseflugAuthor Commented:
Eric -

Thanks - I am an idiot - can you show me how I would plug something like this into my existing script?  Thanks!!!!!
0
 
tbaseflugAuthor Commented:
Is it possible to just copy a directory, and by doing so, copy over all sub directories and files included within?
0
 
heintalusCommented:
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
                DestDir.Create()
            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)
                Else
                    ' 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
            Next

            ' 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)
            Next
        Else
            Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName)
        End If
    End Sub

HTH
Andy
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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