tbaseflug
asked on
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\REPOR TING\CONNE CTIONS\Sup port_GP.ds n"
Dim sDestinations() As String = Array.CreateInstance(GetTy pe(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
'------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
'-------------------------
Sub FileCopy2()
Dim sSorurce As String = txtFileSource.Value
Dim sFolder As String = "\c$\INETPUB\WWWROOT\REPOR
Dim sDestinations() As String = Array.CreateInstance(GetTy
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
'-------------------------
ASKER
Eric -
Thanks - I am an idiot - can you show me how I would plug something like this into my existing script? Thanks!!!!!
Thanks - I am an idiot - can you show me how I would plug something like this into my existing script? Thanks!!!!!
ASKER
Is it possible to just copy a directory, and by doing so, copy over all sub directories and files included within?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim al As ArrayList = New ArrayList
'Search current directory.
For Each f As String In System.IO.Directory.GetFil
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.GetDir
For Each f As String In System.IO.Directory.GetFil
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