Unique Name Generation

  • Thread starter Thread starter Jerry Camel
  • Start date Start date
J

Jerry Camel

I need to be able to generate unique names for files. I was considering
that hash alogorithms, but if I had two files with the same name, I'd get
the same hash. I am collecting and storing files and I want to store them
all in the same directory and I want the names to be cryptic and unique.
Any thoughts?

Jerry
 
Jerry said:
I need to be able to generate unique names for files. I was
considering that hash alogorithms, but if I had two files with the
same name, I'd get the same hash. I am collecting and storing files
and I want to store them all in the same directory and I want the
names to be cryptic and unique. Any thoughts?

I needed to do this in the VB3 / 8.3 era for an image capture program.
There was the possibility of many images from many computers on any given
day, so I came up with a base-36 naming system something like this. BTW,
it's been a long time ago so please don't flame for math errors. <g>

Base 36 code:
0 = 0
1 = 1
....
9 = 9
A = 10
B = 11
....
Z = 36

The 8 part of the 8.3 filenames were of this form:
YMDZZXXX

Y -- Year. 0 = 1990, 1=1991, ..., Z = (1990 + 36)
M -- Month. 1 = Jan, 2 = Feb, ... (1-based for ease of reading)
D -- Day 1 = 1, ... (1-based for ease of reading)
ZZ -- "Assigned Computer Number" 36 * 36 possibilities
XXX -- Sequence #. 36 * 36 * 36 possibilities

The program looked in an ini file for the next sequence # for that day,
generated the 8 part of the 8.3 filename, checked the directory, if already
existed incremened & tried again, repeatedly until ZZZ+1 before failing,
saved the full 8.3 (JPG, TIF, WSQ or BMP), updated the ini file.

It worked well. Images captured in the mid 1990's are still part of a
fingerprint biometric test database, I believe.

I think it would satisfy your "cryptic and unique" requirements. <g>

-- Mark
 
Mark said:
BTW, it's been a long time ago so please don't flame for math
errors. <g>

Or programmer memory errors either... ;-) I recall now it was a little
more cryptic. I used the first 7 chars to define a "family" of files, all
in the same "enrollment." The eighth character specified which image and
impression, ie "Face, Right Profile #2" or "Right Index Finger, 3rd
impression." What the eighth character stood for was saved in a ini file
named the first 7 chars .TXT.
The 8 part of the 8.3 filenames were of this form:
YMDZZXXX

Almost. Should have been
YMDZZXXA

XX -- Sequence #. 36 * 36 possibilities
A -- "Which Image" designator. 36 possibilites. Saved in
the "family's" .TXT file.

FWIW... <G>

-- Mark
 
I need to be able to generate unique names for files.

Use GUIDs. They are guaranteed to be unique, and they
are cryptic. Easiest way to generate a GUID is to
ask for one from WinAPI:

--clip--
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type

Private Declare Function CoCreateGuid Lib "OLE32.DLL" _
(pGuid As GUID) As Long

Private Function GetGUID() As String
Dim udtGUID As GUID

If (CoCreateGuid(udtGUID) = 0) Then
GetGUID = _
String(8 - Len(Hex$(udtGUID.Data1)), "0") & Hex$(udtGUID.Data1) & _
String(4 - Len(Hex$(udtGUID.Data2)), "0") & Hex$(udtGUID.Data2) & _
String(4 - Len(Hex$(udtGUID.Data3)), "0") & Hex$(udtGUID.Data3) & _
IIf((udtGUID.Data4(0) < &H10), "0", "") & Hex$(udtGUID.Data4(0)) & _
IIf((udtGUID.Data4(1) < &H10), "0", "") & Hex$(udtGUID.Data4(1)) & _
IIf((udtGUID.Data4(2) < &H10), "0", "") & Hex$(udtGUID.Data4(2)) & _
IIf((udtGUID.Data4(3) < &H10), "0", "") & Hex$(udtGUID.Data4(3)) & _
IIf((udtGUID.Data4(4) < &H10), "0", "") & Hex$(udtGUID.Data4(4)) & _
IIf((udtGUID.Data4(5) < &H10), "0", "") & Hex$(udtGUID.Data4(5)) & _
IIf((udtGUID.Data4(6) < &H10), "0", "") & Hex$(udtGUID.Data4(6)) & _
IIf((udtGUID.Data4(7) < &H10), "0", "") & Hex$(udtGUID.Data4(7))
End If

End Function
--clap--
 
* Petrik Salovaara said:
Use GUIDs. They are guaranteed to be unique, and they
are cryptic. Easiest way to generate a GUID is to
ask for one from WinAPI:

The easiest way to create a GUID in .NET is to use the 'System.Guid'
class...

;-)
 
Thank you all for your thoughts... I was going to go with GUIDs, but I just
ended up concat'ing the milliseconds,seconds,minutes,hours,year,month and
day from Now(). It's pretty much guaranteed to be unique as it will only
run on one system. And what are the chances of two people generating a file
at exactly the same millisecond, anyway? Thanks.

Jerry
 
just take ticks property, allready containing all that
every tick is 0.1ms, so you should create 2 files in the same 100nanoseconds
:-)

but to be really sure, add the pc name of the pc guid ;-)
that will be the ultimate unique number ;-)


dominique
 
Custom algorithms and GUIDs are cool, but I prefer to use system provided
functionality when it provides exactly what you need.

Check out the System.IO.Path's GetTempFileName method.

I've used the Win32 API equivalent before with flawless results.
 
* "Andrew J. Marshall said:
Custom algorithms and GUIDs are cool, but I prefer to use system provided
functionality when it provides exactly what you need.

Check out the System.IO.Path's GetTempFileName method.

That's what I was looking for when doing some "research" in the
documentation. Nevertheless, these filenames won't be really "unique"
(only unique on the machine).
 
yes but :-)
I also checked that to give the guy an answer
i think that solution is not good enough because you cannot set the
destination directory
i used that also a lot in java, there you can specify your
directory/prefix/suffix of the file
much better
but besides of that i completely agree with you


and also this java-dotnet difference is again one of the strange "why didn't
they copy that feature"
i always see a lot of things copied from java, and then very nice java
features which are not available...


dominique
 
Very cool. Thanks.

Jerry

Andrew J. Marshall said:
Custom algorithms and GUIDs are cool, but I prefer to use system provided
functionality when it provides exactly what you need.

Check out the System.IO.Path's GetTempFileName method.

I've used the Win32 API equivalent before with flawless results.
 
Actually the filename will probably only be unique to the path 8-(

Jerry never stated his uniqueness scope, so ... ?
 
* "Andrew J. Marshall said:
Actually the filename will probably only be unique to the path 8-(
ACK.

Jerry never stated his uniqueness scope, so ... ?

Who knows...
 
Actually, Jerry mentioned that all the files would be stored in the same
directory... :o)
All the feedback has been great and this problem is a problem no more...
Thanks!

Jerry

Andrew J. Marshall said:
Actually the filename will probably only be unique to the path 8-(

Jerry never stated his uniqueness scope, so ... ?
 
Back
Top