udfFileCreateGuidTemp
file udfFileCreateGuidTemp (str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileCreateGuidTemp (strLeading, strTrailing)
If strLeading != "" || strTrailing != ""
   arrChars = Arrayize ("34,60,62,124,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,58,42,63,92,47,0", ",")
   intChars = ArrInfo (arrChars, 1) - 1
   For intChar = 0 To intChars
      arrChars[intChar] = Num2Char (arrChars[intChar])
   Next
   strForbiddenChars = ArrayToStr (arrChars)
   strLeading = StrClean (strLeading, strForbiddenChars, "", @TRUE, 1)
   strTrailing = StrClean (strTrailing, strForbiddenChars, "", @TRUE, 1)
EndIf
strFilenameMask = ShortCutDir ("Local Settings") : "\Temp\" : strLeading : "{1}." : strTrailing
intBBSize = 16
hdlBB = BinaryAlloc (intBBSize)
BinaryEodSet (hdlBB, intBBSize)
intCount = 0
While @TRUE
   intCount = intCount + 1
   If intCount > 100 Then ErrorEvent (-1, 7777, "udfFileCreateGuidTemp: We tried " : intCount - 1 : " times, but cannot create unique temporary file name.")
   DllCall (StrCat (DirWindows (1), "OLE32.DLL"), long : "CoCreateGuid", lpbinary : hdlBB)
   strFilename = StrReplace (strFilenameMask, "{1}", BinaryPeekHex (hdlBB, 0, intBBSize))
   If FileExist (strFilename) != 0 Then Continue
   BinaryWriteEx (hdlBB, 0, strFilename, 0, -1)
   If FileExist (strFilename) != 0 Then Break
EndWhile
hdlBB = BinaryFree (hdlBB)
Return strFilename
;..........................................................................................................................................
; This UDF "udfFileCreateGuidTemp (strLeading, strTrailing) creates a 0-byte file with an unique name
; into the user's temp folder (i. e. subfolder "Temp" within the folder "Local Settings").
;
; The filename will be assembled from three parts: the leading string, the GUID string, the trailing string.
;
; The GUID string allows, almost collision free, the creation of a numerous number of files in the one temporary folder.
;
; If there would occur any collision (what would be rather unusual), then at first the function will try to create another filename.
; The function will try maximal 100 times, but then throw minor error 7777.
;
; Detlev Dalitz.20101008.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strFilename01 = udfFileCreateGuidTemp ("", "")                         ; "...\Temp\99C849508654FF4599BD14824593A79C
strFilename02 = udfFileCreateGuidTemp ("P", "")                        ; "...\Temp\P665775041629B647AD8D715FB76669B1
strFilename03 = udfFileCreateGuidTemp ("Pr", "")                       ; "...\Temp\Pr620F88C206BAFC4CA42A1A78E1C3D9D9
strFilename04 = udfFileCreateGuidTemp ("Pre", "")                      ; "...\Temp\Pre6D4BE2427BCEE046894A0C02444F68E7
strFilename05 = udfFileCreateGuidTemp ("Prefix", "")                   ; "...\Temp\Prefix90DCF5B83B9A8648B68B70D8053C9B20
strFilename06 = udfFileCreateGuidTemp ("P", "t")                       ; "...\Temp\PE21EF912E121494DAF57905882536B37.t
strFilename07 = udfFileCreateGuidTemp ("", "t")                        ; "...\Temp\C2317B17EB0E0941B99AC6857D3721F3.t
strFilename08 = udfFileCreateGuidTemp ("", "tx")                       ; "...\Temp\841194D61C58DA44A372197CEFDC0EF7.tx
strFilename09 = udfFileCreateGuidTemp ("", "txt")                      ; "...\Temp\04BB6583AAA1BC4B8EA807DA5988FBEA.txt
strFilename10 = udfFileCreateGuidTemp ("Txt", "txt")                   ; "...\Temp\Txt12590F8D5C98F34EB58E97E400743883.txt
strFilename11 = udfFileCreateGuidTemp ("Txt", "special extension.txt") ; "...\Temp\Txt0A5A3FE653FDAC4AA97F1CB3DA104A5D.special extension.txt
strFilename12 = udfFileCreateGuidTemp ("A\?D.", "<>.tmp")              ; "...\Temp\AD.B99578301791234AB25E3B6560EC23A6..tmp
strFilename12 = udfFileCreateGuidTemp ("Test.", "last.one.tmp")        ; "...\Temp\Test.4F1393A6D08F904AA74AA374159BDE92.last.one.tmp

:CANCEL
Exit