tags:

views:

174

answers:

5

My app needs to copy/move thousands of files throughout the day to a directory. I need to ensure that when I copy over these files I rename them to something that is unique for that directory.

I have no requirements for the names other than they need to obviously be unique.

What is the proper way to handle this? Should I use some kind of GUID, some incrementing number or some other method?

How would I do this in C#?

+3  A: 

Use a GUID. Details are coming.

http://articles.techrepublic.com.com/5100-10878_11-5708732.html

Hamish Grubijan
+6  A: 

I tend to use

System.Guid.NewGuid().ToString() + ".jpg"

when the only requirement is that they are absolutely required to be guaranteed to be unique.

This is, of course, already one of your ideas, so +1 to you for thinking of it first.

David Stratton
Guid.NewGuid(), not NewId().
Kyralessa
Thank you, @Kyralessa!
David Stratton
+10  A: 

You can use Path.GetRandomFileName

However I don't think the generated filenames are guaranteed to be unique (they are generated using RNGCryptoServiceProvider), so a GUID might be a better idea

Thomas Levesque
So just wrap this is a File.Exists() loop to ensure that file doesn't exist and if not, create it?
Sinilax
Yes, you could check for an existing file with the same name...
Thomas Levesque
The problem with File.Exists() is it leads to race conditions. Just put the rename process in a try-catch block, and if an exception is thrown then just do it again. You'll have to research which type it is when a file already exists, as I can't remember off the top of my head.
Phong
A: 

Hi Sinilax,

You can safely use GUID's. They are unique, and a filename can get generated with

String filename = Guid.NewGuid().ToString() + ".MyExtension";

br, Marcel

Marcel
Note that GUID are not guaranteed to be unique... there is just a very low probability of generating the same GUID twice
Thomas Levesque
Thomas, you are right. The probability is however so small, that it probably takes longer to wait for the end of the world than for a non-unique Guid coming out the Guid class. :-)
Marcel
In any case, I would check if the file exist, and put that in a loop with x repetition. I know that the probability are small, but it's there and it should be handled correctly.
Patrick Parent
A: 

If you need the new file name to resemble the old one you could use this solution:

string oldFileName = @"c:\old\oldfile.txt";

string newFileName = oldFileName.Substring(0, oldFileName.LastIndexOf('.')) + "-";

string oldFileNameExtension = oldFileName.Substring( oldFileName.LastIndexOf('.')+1 );

File.Move(oldFileName, newFileName + DateTime.Now.Ticks.ToString() + oldFileNameExtension);

DateTime.Ticks is an integer of type Long which contains the current date converted to nanoseconds. Beware: most CPU's can perform many operations before the Ticks counter is advanced.

If you are doing many files at once you will have to add a while(File.Exists ()) loop with a Thread.Sleep(10); in it. Or if you don't want to slow it down with Thread.Sleep you could add a counter to the loop and append that to the end of newFileName.

John S