Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
My team is supporting a system that does multiple file processing. Basically, it gets the data from somewhere, and saves it in a file, and is then transmitted to another system. Currently, it is using Date/Time stamp for the filenames. The problem with it is that, when there are many files to be processed, some files get the same filename. It seems that we really can't rely on the timestamps, even if we have included milliseconds on it. I am thinking of using GUID for the generation of filenames, to ensure they will be unique. My question would be, is there a better way you would recommend than to use GUID or shall I push through with this approach?
Posted

I would be tempted to go with both: a date stamp with a GUID attached:
20110727-cdd0d9b6-2e29-41d0-b1f7-dd38f87971b7
The extra characters shouldn't cause a problem, but they do make it easier to search...
 
Share this answer
 
Comments
Reiss 27-Jul-11 11:42am    
I like that suggestion - maybe create a directory with the date as the name and then add files using a pure GUID.
I have always gone with the
C#
string filename = System.Guid.NewGuid().ToString();

approach - I usually also create directories using this approach too as the clean up time is a lot quicker as you can delete a whole directory rather than individual files
 
Share this answer
 
Guid.NewGuid() is generally best, SQL FileStream uses this method for storing it's files
 
Share this answer
 
File.CreateTempFile [^] can be used to create unique file names, but only 65535, and only if you leave the files with that name in the temp folder. A GUID is unweildy, but that's why it's guarenteed unique. If you want shorter filenames, you can shorten the GUID, but then you need to check for another file of the same name, and if you're doing that, you may as well create files by hand. Another option is to use a number sequence and store it in your DB, if you have one. Then you can make a transaction wrapped call to increment that number for each file.
 
Share this answer
 
Comments
walterhevedeich 27-Jul-11 21:22pm    
Thanks. I think I'll go with the CreateTempFile approach. It seems to generate shorter filenames than when I use GUID.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900