Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How create a unique file name in VB 6? 1

Status
Not open for further replies.

cl8855

Programmer
Nov 28, 2000
47
US
The search is down, so I will just start a new thread -

How can I create a unique file name within VB 6.0? On other platforms we just use a timestamp down to milliseconds, but the time function doesn't go that far on VB 6.0. Anyone have a way around the timestamp and/or a way to create a unique name every time (across multiple users and application executions!)

thanks,
 
how unique is unique...
i just use the user id or name and the time(hour/minute/second) then filter out any nasty characters.

its not pretty but it gets the job done!
 
Try this or a variation on it....

Format(now, "DHMMSS") Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 
Thanks,
userid added in might work if I have access to that at the time of execution - no 1 user should be able to do 2 in one second....
 
to return the user just use GetUserName api (i think its getusername anyways cant remember of the top of my head)

(just have a look at the api viewer under get<whatever>)

now returns dd/mm/yy hh:mm:ss so if you used username and now there still may be a duplicate name

oo actually there may be a getsystemtime api call that returns milliseconds again check api viewer

in the meantime ill have a look around

hope this helps
 
give this a whirl... hopefully ive typed it correct

Private Declare Function GetUserName Lib &quot;advapi32.dll&quot; _
Alias &quot;GetUserNameA&quot; ( _
ByVal lpBuffer As String, _
nSize As Long _
) As Long

Private Declare Sub GetSystemTime Lib &quot;kernel32&quot; _
(lpSystemTime As SYSTEMTIME )

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Dim szNameBuffer As String * 6
Dim nSize As Long
Dim tpTimeData As SYSTEMTIME
Dim MyTime as String
Dim MyUniqueID as String

Private Sub Form_Load()

nSize = Len(szNameBuffer)

Call GetUserName(szNameBuffer, nSize)

Call GetSystemTime(tpTimeData)

MyTime = cstr(tpTimeData.wHour & tpTimeData.wMinute & _
tpTimeData.wSecond & tpTimeData.wMilliseconds)

MyUniqueID = szNameBuffer & MyTime

End Sub
 
ADoozer et al,

Be aware that GetSystemTIme() does not guarantee different results on calls that occur less than ~50msec apart. This is similar to the resolution of the old DOS clock. A reference to this behaviour is buried in an obscure note of the MSDN.
 
this is true, however it may be slightly better than using 'now' as it offers an option to use milliseconds.

as i said earlier its not pretty but it will (mostly) do the job. as cl8855 says no user &quot;should&quot; be able to do 2 whatevers in a second.

ps i also limited the return from getusername and if 2 users have the same names (Simone Joe, Simone Jack) and do the process in the same alloted second there may be a problem.

in the meantime if someone can offer nicer code that will 'definately' work i would also like to see it.
 

How about using the date (yyyymmdd) and querypeformancecounter API?

Just a thought...

Good Luck

 
will try it tomorrow and update record then,
thanks all
 
vb5prgrmr: any chance of a quick function showing the queryperformancecount... ive read the msdn bumf but am not sure about this lpperformancecount, in particular &quot;if the hardware doesnt support a high resolution performance counter&quot; how do we tell if it is supported.

cl8855: sorry for hogging your thread!
 

Use the QueryPerformanceFrequency API ... from help...

Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

Good Luck

 
sorry... that really was a dumb post... but i like your idea and hence you get a star.

anyone lazy like me can just cut and paste

Private Declare Function QueryPerformanceCounter Lib &quot;kernel32&quot; _
(lpPerformanceCount As LARGE_INTEGER) As Long

Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Dim BigNumber As LARGE_INTEGER

Private Function UniqueID()

result = QueryPerformanceCounter(BigNumber)

If result <> 0 Then
UniqueID = Format(Now, &quot;ddmmyy&quot;) & BigNumber.lowpart & BigNumber.highpart
End If
End Sub
 
>an obscure note of the MSDN

But. if you've been in this forum for any length of time, you will have seen repeated reminders about this issue (and the actual resolution depends on the OS)
 

ADoozer,

Actually that should be...(for your example)
[tt]
Dim MyValue As Varient
MyValue = CDec(4294967296#)
UniqueID = Format(Now, &quot;ddmmyy&quot;) & Trim(Str(CDec((MyValue * StopInt.highpart) + StopInt.lowpart)))
[/tt]

strongm,

Yes you are so right but when the hardware does support this API (have not meet a system yet that does not) and you make a call to it twice in a row you should still get a difference of at least 1 which in turn would make the file name unique if you include the date as part of the file name for when the counter rolls over (I am assuming that it does roll over since there is a limit to how large the LARGE_INTEGER is), don't you think?

Good Luck


 
I have a function for u cl8855 .. try it, and let me know..
'==========================================================
'API
Private Declare Function GetTempFileName Lib &quot;kernel32&quot; Alias &quot;GetTempFileNameA&quot; (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
'==========================================================
Private Function GenTempName(sPath As String)
Dim sPrefix As String
Dim lUnique As Long
Dim sTempFileName As String
sPrefix = &quot;fVB&quot;
lUnique = 0
sTempFileName = Space$(100)
GetTempFileName sPath, sPrefix, lUnique, sTempFileName
sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1)
GenTempName = sTempFileName
End Function
'==========================================================
and use it like this
'==========================================================
Private Sub Form_Click()
MsgBox GenTempName(&quot;C:\Documents and Settings\Administrator\Desktop\RMS Ppt&quot;) 'change To a valid path
End Sub
'========================================================== All the Best
Praveen Menon
pcmin@rediffmail.com
 
vb5prgrmr: sorry should of said, that was not meant as a final solution. dont fully understand where your additional/replacement code goes. when i wrote that i was just mucking around with a text box and command button and it worked.
 
This might seem like an odd remark (and if implied will certainly lead to difficult to read filenames), but they claim GUIDs to be unique to a very high degree of certainty.
Can you not just use CoCreateGuid?
Greetings,
Rick
 
Oddly, I posted a CoCreateGuid solution to this the other day - but it somehow never made it from my browser window into the thread...
 
OK, looks like the CoCreateGuid would be my best choice, but I am not familiar with the SDK functions -

From the MSDN:
HRESULT CoCreateGuid(

GUID * pguid

);

I don't know how I use this type (hresult?) of function...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top