• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Using - Copy(FileToCopy, NewCopy)


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.

  • 3
  • 2
1 Solution
Mohamed AbowardaSoftware EngineerCommented:
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.
John500Author Commented:
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

John500Author Commented:
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.
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Mohamed AbowardaSoftware EngineerCommented:
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.
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

John500Author Commented:
>>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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now