6

I copy image files from one folder to another with help of File.Copy() method.

I need to give unique file name to the copied image file before it is pasted to the folder.

Any idea how I can implement this?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Michael
  • 13,950
  • 57
  • 145
  • 288
  • 3
    See: http://stackoverflow.com/questions/1602578/c-what-is-the-fastest-way-to-generate-a-unique-filename – Emond Jul 29 '12 at 08:46
  • 1
    I don't think that's an exact duplicate, because it asks for the *fastest* way, which probably isn't the best, most simple and maintainable way. – Anders Abel Jul 29 '12 at 08:57

5 Answers5

5

You can use Path.GetTempFileName

more info at: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.100).aspx

a quote from the link:

"Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."

eyossi
  • 4,230
  • 22
  • 20
  • This would be good for creating a temp file to write temp data in to. It is not ideal for simply generating a unique name. – HackSlash Jul 09 '18 at 22:28
  • @HackSlash correct. My answer is not the correct one since the file can be deleted as it is a temporary file. (Upvoted the better answers below) – eyossi Aug 06 '18 at 18:30
5

Eyossi's answer should be the accepted solution but another alternative:-

You could use a guid to create a unique name ("Such an identifier has a very low probability of being duplicated."):

Dim filename As String = Guid.NewGuid().ToString + ".png"
winwaed
  • 7,645
  • 6
  • 36
  • 81
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Sorry - I was working in C#. I think my brain must have automatically removed the Dim *** As String :-) – winwaed Jul 22 '14 at 12:28
  • A GUID was not designed for this purpose where `Path.GetRandomFileName()` was. The result is a similarly incomprehensible string. – HackSlash Jul 09 '18 at 22:27
4

Here's another way to do it

fileName = User_Appended_File_Name & "_" & DateTime.Now.ToString("yyyyMMdd_HH_mm_ss")

It would create a fairly identifiable File Name, with the added advantage of knowing when it was created, a big ol' timestamp.

edit: It will not work if the file saving rate is faster than 1second.

  • 5
    This fails completely if two files are created in the same second, which could happen quite frequently. – Andrew Barber Jul 30 '12 at 17:00
  • ^ agreed but for scenario's that take longer than a second (ie exporting to excel file format on a local PC) this works well. Plus you can sort the files in the folder grouped by file type. – Jeremy Thompson Mar 26 '19 at 00:08
3

I recommend using Path.GetRandomFileName().

The previously accepted answer was to use Path.GetTempFileName which creates a file. This is not ideal when that file is going to be the target of a file copy operation, as stated in the question.

Unlike GetTempFileName, GetRandomFileName does not create a file. When the security of your file system is paramount, this method should be used instead of GetTempFileName.

SOURCE: https://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename(v=vs.110).aspx

EXAMPLE:

Imports System.IO

Module Module1

    Sub Main()
        Dim fileName = Path.GetRandomFileName()
        Console.WriteLine("Random file name is " & fileName)
    End Sub

End Module

' This code produces output similar to the following:   
' Random file name is w143kxnu.idj
HackSlash
  • 4,944
  • 2
  • 18
  • 44
0

You may use current date-time as the file name.

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69
  • 4
    You should note that this would fail if two files are created during the same second, or whatever the smallest time increment indicated by the timestamp is. – Andrew Barber Jul 30 '12 at 17:01