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

Dani AI

Generated

As pointed out and confirmed, the failure came from building a target path that contained characters Windows does not allow (and from concatenating parts in a way that made a filename look like a folder). A robust pattern is to (1) build names with a fixed, filename-safe timestamp format, (2) sanitize the original name using .NET helpers, (3) use Path.Combine to join segments, and (4) ensure the destination folder exists before copying.

' Remove invalid filename characters
Private Function MakeSafeFileName(name As String) As String
    Dim invalid = Path.GetInvalidFileNameChars()
    For Each c As Char In invalid
        name = name.Replace(c, "_"c)
    Next
    Return name
End Function

' Copy file into versions\<baseName>\baseName_timestamp.ext
Public Function CopyWithTimestamp(sourcePath As String, versionsRoot As String, Optional overwrite As Boolean = False) As String
    Dim original = Path.GetFileName(sourcePath)
    Dim baseName = Path.GetFileNameWithoutExtension(original)
    Dim ext = Path.GetExtension(original)
    Dim timeStamp = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")   ' safe format: no ":" or "/"
    baseName = MakeSafeFileName(baseName)
    Dim destDir = Path.Combine(versionsRoot, baseName)
    If Not Directory.Exists(destDir) Then Directory.CreateDirectory(destDir)
    Dim destFile = baseName & "_" & timeStamp & ext
    Dim destPath = Path.Combine(destDir, destFile)
    File.Copy(sourcePath, destPath, overwrite)
    Return destPath
End Function

Troubleshooting notes: prefer Path.GetInvalidFileNameChars to hand-rolling Replace calls; use Directory.CreateDirectory to guarantee the folder exists; wrap File.Copy in Try/Catch to handle FileNotFoundException, IOException, UnauthorizedAccessException, PathTooLongException and log the exact exception message. Avoid naming variables that shadow framework members (for example, do not use the identifier Now for a variable). Also remember older Windows/.NET path length limits (MAX_PATH) when composing very long versioned names; if needed implement a shorter timestamp or a numeric sequence instead.

Recommended Answers

All 2 Replies

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

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.