Perform a Looping file.copy

I am trying to perform a simple file.copy - however, I want to perform the action for multiple destinations - is there a way to loop through and perform an action for each of the  below

        Dim sSorurce As String = "\\SERVER1\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"

        Dim sDestination1 As String = "\\SERVER2\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
        Dim sDestination2 As String = "\\SERVER3\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
        Dim sDestination3 As String = "\\SERVER4\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"

        File.Copy(sSorurce, sDestination1, True)
        File.Copy(sSorurce, sDestination2, True)
        File.Copy(sSorurce, sDestination3, True)


What I would like to do, in place of having to perform an explicit file.copy for each destination - somehow loop through complete it for each defined location / destination

tbaseflugAsked:
Who is Participating?
 
lengreenCommented:
Hi tbaseflug,

i assume your servers are not numbered 1 , 2 , 3 etc


Dim sSorurce As String = "\\SERVER1\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
Dim sDestinations() as String

sDestinations(0) = "\\SERVER2\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
sDestinations(1) = "\\SERVER3\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
sDestinations(2) = "\\SERVER4\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"


Dim sDestination as String

For each sDestination in sDestinations
    File.Copy(sSorurce, sDestination, True)
Next


Cheers!
0
 
lengreenCommented:
tbaseflug,

sorry line should be

Dim sDestinations() As String = Array.CreateInstance(GetType(String), 3)
0
 
tbaseflugAuthor Commented:
lengreen -

This is what I have in total (below) - but am getting the following error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 115:
Line 116:
Line 117:        sDestinations(0) = "\\LINKSERVER-6030" & sFolder
Line 118:        sDestinations(1) = "\\LINKSERVER-6050" & sFolder
Line 119:        sDestinations(2) = "\\LINKSERVER-6070" & sFolder
 
Source File: c:\inetpub\wwwroot\SandBox2\FileCopy.aspx.vb    Line: 117
'---------------------------------------
        Dim sSorurce As String = "\\LINK-DDASH\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
        Dim sFolder As String = "\c$\INETPUB\WWWROOT\REPORTING\CONNECTIONS\Field_GP.dsn"
        Dim sDestinations() As String

        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

        For Each sDestination In sDestinations
            File.Copy(sSorurce, sDestination, True)
        Next
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
tbaseflugAuthor Commented:
lengreen -

Looks like the mod that you posted might work - let me test it out - thanks!!
0
 
RonaldBiemansCommented:
the array isn't initialized

like

Dim sDestinations(45) As String

or with the redim

Dim sDestinations() As String
rediim sDestinations(10)

if you want to change increase the array but want the data preserved use

redim preserve
 
0
 
lengreenCommented:
hi again

> Dim sDestinations() As String = Array.CreateInstance(GetType(String), 3)

the 3 is the size of the array, BTW so looks like you need to set it to 46
0
 
tbaseflugAuthor Commented:
Thanks - let me give it a shot - I know that this is a little out of the original bounds - but is there anyway to catch an error and tell me if it fails for a specific server as well as to continue on with the next, etc?
0
 
lengreenCommented:
tbaseflug,

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
0
 
lengreenCommented:
tbaseflug,

Im away now, good luck

cheers

Len
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.

All Courses

From novice to tech pro — start learning today.