;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetLogName() 
;; 
;;ACTION         Returns a guaranteed-unique file name for logging or temp files. 
;;               Returns a unique file name by adding a sequence number 
;;               between the specified FilePath and its extension. If no 
;;               extension is specified, a default of ".log" will be used. 
;;               The sequence number is NumDigits in length (default is 2).  
;;               If DateStamp is specified, the Date (_YYYYMMDD) is inserted 
;;               between the base file name and the sequence number. 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2002/07/23 
;; 
;;HISTORY        1.0  - 2002/07/23 - Initial Release 
;; 
;;SYNTAX         GetLogName(FilePath [, NumDigits, DateStampFlag]) 
;; 
;;PARAMETERS     FilePath - Required - String 
;;               - Path and name of file 
;;                
;;               NumDigits - Optional - Integer 
;;               - Number of digits in the sequence number 
;;                
;;               DateStampFlag - Optional - Integer 
;;               - When true, datestamps the filename 
;; 
;;REMARKS         
;; 
;;RETURNS        The FilePath parameter with a sequence number inserted in it. 
;;               This function insures that a unique, sequenced file name is 
;;               created in the specified location. 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    NT4, W2K, WXP 
;; 
;;EXAMPLES       $LogFile = GetLogName("c:\temp\junk.txt") 
;;               	returns "c:\temp\junk.##.txt", where ## is unique and sequential 
;;               $LogFile = GetLogName("c:\temp\junk.txt", 4, 1) 
;;               	returns "c:\temp\junk_YYYYMMDD.####.txt" 
; 
Function GetLogName($_FilePath, Optional $_NumDigits, Optional $_DateStampFlag)
 
  Dim $_DateStamp, $_Seq, $_File, $_BaseFilePath, $_FilePathExt, $_P, $_Max
 
  ; Break the FilePath into Path\File and Extension parts 
  ; use a default .LOG extension if one wasn't specified 
  $_P = InStrRev($_FilePath, '.')
  If $_P = 0
    $_BaseFilePath = $_FilePath
    $_FilePathExt  = '.log'
  Else
    $_BaseFilePath = Left($_FilePath, $_P - 1)
    $_FilePathExt  = '.' + Right($_FilePath, Len($_FilePath) - $_P)
  EndIf
 
  ; Define DateStamp - either NULL or "_YYYYMMDD" 
  $_DateStamp = IIf($_DateStampFlag = 0, '', '_' + Join(Split(@DATE, '/'), ''))
 
  ; Set NumDigits to 2 if not specified, otherwise 
  ; insure it is positive and less than 6 
  $_NumDigits = IIf($_NumDigits = '', 2, Abs($_NumDigits))
  $_NumDigits = IIf($_NumDigits > 6, 6, $_NumDigits)
 
  ; Define the maximum value of the sequence number 
  $_Max = Val(Left('999999', $_NumDigits))
 
  ; Process each possible sequence in the file name 
  For $_Seq = 0 to $_Max
    $_File = $_BaseFilePath + $_DateStamp + '.' + Right('000000' + CStr($_Seq), $_NumDigits) + $_FilePathExt
    If Not Exist($_File)
      $_Seq = 1000000
    EndIf
  Next
 
  If $_Seq = 1000001
    $GetLogName = $_File
    Exit 0
  Else
    $GetLogName = ''
    Exit 18
  EndIf
 
EndFunction