?
Solved

Perform a Looping file.copy

Posted on 2005-04-22
9
Medium Priority
?
173 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:tbaseflug
  • 5
  • 3
9 Comments
 
LVL 10

Accepted Solution

by:
lengreen earned 2000 total points
ID: 13843465
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
 
LVL 10

Expert Comment

by:lengreen
ID: 13843607
tbaseflug,

sorry line should be

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

Author Comment

by:tbaseflug
ID: 13843620
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tbaseflug
ID: 13843652
lengreen -

Looks like the mod that you posted might work - let me test it out - thanks!!
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13843667
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
 
LVL 10

Expert Comment

by:lengreen
ID: 13843679
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
 

Author Comment

by:tbaseflug
ID: 13843726
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
 
LVL 10

Expert Comment

by:lengreen
ID: 13843771
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
 
LVL 10

Expert Comment

by:lengreen
ID: 13843777
tbaseflug,

Im away now, good luck

cheers

Len
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

862 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