I hope it helps you, It is a sub proceedure with with a file open dialog object called "ofd" and the event object is called "cmdcopy" the text on the cmdcopy object is "Copy".

I leave you to fiddle with the snippet.

Private Sub cmdcopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcopy.Click

       Dim filetocopy As String
          Dim stlen As Integer
         filetocopy = ofd.FileName
Dim ext As String
        Dim position As Integer

        ' We first try to get the index of the  "." symbol in the string
        position = InStr(filetocopy, ".")
        stlen = filetocopy.Length
        ext = filetocopy.Substring(position - 1, (stlen - position) + 1)

        'Dim check As Boolean 
        ' Dim location As String
        Dim newfile As String = "c:\newfile78" & ext ' At this point i choose to declare a file 
        If File.Exists(newfile) = True Then
            Dim overwrite As Integer
            overwrite = MessageBox.Show("Do you want to overwrite the existing file", "Overwrite file?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If overwrite = vbYes Then

                File.Copy(filetocopy, newfile)
                MessageBox.Show("Successfully Copied" & vbNewLine & filetocopy & "to" & newfile)
            ElseIf overwrite = vbNo Then
                Exit Sub
            End If

        End If
        File.Copy(filetocopy, newfile & ext)
        MessageBox.Show("Successfully Copied" & vbNewLine & filetocopy & "to" & newfile)
End Sub
9 Years
Discussion Span
Last Post by Teme64

You may find My.Computer.FileSystem very useful. It has methods for FileCopy, FileExists and GetFileInfo, which in turn provides Length and Extension of the file.

A few scenarios where the "copy file"-code above does not work:
- "C:\Temp\myfile" (no extension)
- "C:\foo.bar\myfile" (dot in the path)
- "C:\Temp\.myfile" (dot as the first char in file name)

Otherwise the code seems just fine :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.