Hi all!
i am trying to copy a file from 1 location to other location with a new name. The new name should be like "Oldname + date & time". I wrote the following code but getting error that the Given path format is not supported

Imports System.IO

Public Class Form1
    Dim file_name As String = "jpeg.rtf"
    Dim Now As DateTime = Date.Now

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path1 As String = Trim("D:\frs\files\" & file_name)
        Dim path2 As String = Trim("D:\frs\versions\" & file_name & "\" & Now & file_name)
        File.Copy(path1, path2)
        MsgBox("D:\frs\versions\" & file_name & "\" & Now & file_name)

    End Sub
End Class
Imports System.IO

Public Class Form1

    Private file_name As String = "jpeg.rtf"
    Private tempString As String = Date.Now

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '/////////////////////////////
        tempString = tempString.Replace("/", "-")
        '// unless you replace these character, the file name returns invalid and causes errors.
        tempString = tempString.Replace(":", ".")
        '////////////////////////////
        Dim path1 As String = Trim("D:\frs\files\" & file_name)
        Dim path2 As String = Trim("D:\frs\versions\" & tempString & " " & file_name) '// modified.
        File.Copy(path1, path2)
        MsgBox("File copied to:" & vbNewLine & path2)
        End
    End Sub
End Class

As mentioned, you are trying to create a file with invalid characters for the file name.
Also, you are trying to copy the file to a folder that does not exist so I modified the code in path2.

One more thing, your declared variable "Now" might conflict with the vb.net code for Now.
Try using something that does not cause/or could cause conflicts.

Hope this helps.

commented: Very accurate reply. +1

@codeorder thanks. I got it. I thought we can use '/' . And ya the folder where i was coping actually exist. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.