Perform a Looping file.copy

Posted on 2005-04-22
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

Question by:tbaseflug
    LVL 10

    Accepted Solution

    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)

    LVL 10

    Expert Comment


    sorry line should be

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

    Author Comment

    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)

    Author Comment

    lengreen -

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

    Expert Comment

    the array isn't initialized


    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
    LVL 10

    Expert Comment

    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

    Author Comment

    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?
    LVL 10

    Expert Comment


    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)
    LVL 10

    Expert Comment


    Im away now, good luck



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    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…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now