;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetTmpFN() 
;; 
;;ACTION         Returns a unique filespec in the system temp folder 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2003/08/17 
;; 
;;HISTORY        1.0  - 2003/08/17 - Initial Release 
;; 
;;SYNTAX         GetTmpFN(ID) 
;; 
;;PARAMETERS     ID - OPTIONAL - String 
;;               - Used to tie the file to a specific function 
;; 
;;REMARKS        Where supported, it uses the PID macro to provide uniqueness between 
;;               multiple Kix instances.  
;;               The UDF creates a filespec - a path & file name - in the system  
;;               TEMP folder. If the file already exists, it changes part of the 
;;               file name and checks again. It will delay between 0.1 and 0.5 seconds 
;;               between each additional name check. 
;; 
;;               The main purpose of such a function is when many identical instances 
;;               of a script are running at the same time, and a generic temp file 
;;               name causes conflicts. 
;;                
;;               The optional ID argument allows the calling process to add a calling 
;;               function identifier. This may be needed when many functions call 
;;               this function, and you want to ID the source for diagnostic purposes  
;;               or later reference. It is not needed to insure uniqueness. 
;; 
;;RETURNS        Filespec (path\file) to a uniquely named file in the system TEMP folder 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES       $TmpFile = GetTmpFN('TF') ; get a tmp filename for the Test Function 
; 
Function GetTmpFN(OPTIONAL $_FName)
 
  Dim $_Base, $_fT, $_T, $_L, $_P
 
  $_Base = %TEMP% + '\' + $_FNAme + '_'			; base filespec 
  If @PID						; add PID-specific names if supported 
    $_Base = $_Base + @PID + '_'
  EndIf
 
  ; Insure that the filename is unique 
  SRnd(@TICKS)						; Seed the random # generator 
 
  $_T = CStr(@TICKS)					; express ticks as a string 
  $_L = Len($_T) / 4					; length of ID is 25% of TICKS length 
  $_P = Rnd(Len($_T) - $_L) + 1				; random offset into TICKS string 
  $_fT = Val(SubStr($_T, $_P, $_L))			; get a potentially unique ID 
 
  $GetTmpFN = $_Base + $_fT + '.TMP'			; define a file name 
 
  While Exist($GetTmpFN)				; see if it exists 
    Sleep(CDbl(Rnd(4) + 1.0) / 10.0)			; delay 
    $_fT = $_fT + (Rnd(8) + 1)				; Change the ID 
    $GetTmpFN = $_Base + $_fT + '.TMP'			; try another name 
  Loop
 
  Exit 0
 
EndFunction