Using - Copy(FileToCopy, NewCopy)

Posted on 2011-05-11
Last Modified: 2012-05-11

I'm trying to copy 1 file 1,000 times so I can use it in testing a particular system.  The code below only copies once without renaming the file with each iteration of the loop:

' Copy the file to a new location without overwriting existing file.
My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\testFile.txt", _

In other words, the same file should be renamed like this each time the copy takes place:

Copy of testFile.txt
Copy 2 of testFile.txt

Are there other versions of CopyFile(...) with parameters that will do what I want or do I need to manipulate the file name to pull this off?  If I have to change the name with each iteration, please provide the solution using Substring() or any other method that will do the job.

Question by:John500
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    You will have to check if there is file with the same name exists by using System.IO.File.Exists()

    You can also try using:
    System.IO.File.Copy(dest1, dest2, True)

    Open in new window

    Which will throw exception if a file is already exists with the same name.

    If the there is a file with the same name you can add "Copy of " to the beginning of the file name.

    Author Comment

    I just ended up going this route:

    Sub Main()
            Dim check As Boolean = True
            Dim counter As Integer = 0
            Dim DestinationFile As String
                Do While counter < 1001
                    counter += 1
                    ' Copy the file to a new location without overwriting existing file.
                    DestinationFile = "M:\Test\Sub_Directory\MyFile" + counter.ToString + ".txt"
                    My.Computer.FileSystem.CopyFile("M:\Test\MyFile.txt", DestinationFile)
            Catch ex As Exception
            End Try
        End Sub

    Open in new window


    Author Comment

    Any other ideas for something more robust ?  That is, something that will take any file name off the command line along with the destination?

    This might be for another question.
    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    Why don't you check if the file is exists using System.IO.File.Exists()?

    This way will allow you to change the variable "DestinationFile" and retry to copy again.
    LVL 23

    Accepted Solution

    Alternatively you can run the following from the command line:

    for /L %n in (1,1,1000) do @copy myfile.txt "C:\DestinationDir\Copy %n of myfile.txt"

    Open in new window


    Author Comment

    >>Why don't you check if the file is exists using System.IO.File.Exists()?

    Well, the file must exist.  I need to create 1000 copies of the same file in the same directory.


    Although this isn't a VB answer, I have to give you the points for that answer.  That's about as lean as it gets!


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    17 Experts available now in Live!

    Get 1:1 Help Now