Although only one function is presented here, you may notice a gold mine of other associated and required functions that may be used for a multitude of purposes.
Public Function GetUniqueFileName(ThisEXT As String, Optional ThisPath As String) As String
Dim retval As String, varTmp As String
varTmp = NormalizePath(GetTEMPdir(True))
Do
If LTrim(RTrim(ThisPath)) <> "" Then
If DirExistCreate(ThisPath, False) Then
retval = NormalizePath(ThisPath) & UniqueFileName & "." & ThisEXT
ElseIf DirExistCreate(varTmp, True) Then
retval = varTmp & UniqueFileName
End If
DoEvents 'give someone else a chance...
Loop While FindFile(retval)
GetUniqueFileName = retval
End Function
Public Function UniqueFileName() As String
On Local Error GoTo ufnError
Dim retval As String
'This example uses the Rnd function to generate a random integer value from 10000 to 32000.
retval = "~" & Str(Int((32000 * Rnd) + 10000))
ufnOut
UniqueFileName = retval
Exit Function
ufnError
retval = ""
Resume ufnOut
Public Function GetTEMPdir(CreateOne As Boolean, Optional tVar As String) As String
Dim wTmp As String
On Local Error Resume Next
wTmp = Environ$("TEMP")
If wTmp = "" Then
If CreateOne Then
If LTrim(RTrim(tVar)) = "" Then tVar = "C\TEMP"
MkDir tVar
wTmp = tVar
wTmp = NormalizePath(wTmp)
GetTEMPdir = wTmp
Public Function FindFile(ThisFile As String, Optional SetAttribute As Variant) As Boolean
On Local Error GoTo ffError
Dim retval As Boolean, tAttr As Integer
tAttr = vbNormal 'tattr=0
If Trim(ThisFile) = "" Then
Do While tAttr <= 39
If Len(Dir(ThisFile, tAttr)) > 0 Then
If Not IsMissing(SetAttribute) Then
SetAttr ThisFile, CInt(SetAttribute)
retval = True
Exit Do
Else
tAttr = tAttr + 1
'1 -> 7 valid for files
If tAttr = 8 Then tAttr = 32
'32 -> 39 valid for files
If tAttr = 40 Then
retval = False
Loop
ffOut
FindFile = retval
ffError
Msg = "Find File Error " & Error(Err) & vbCrLf
Msg = Msg & ThisFile
MsgBox Msg, vbExclamation, App.EXEName
Err = 0
Resume ffOut
Public Function DirExistCreate(ThisDir As String, Optional CreateIt As Variant) As Boolean
On Local Error GoTo DEerror
Dim Retval As Boolean, varCreateIt As Boolean
Dim ThisTest As String, c As Integer, p As String, i As Integer
Dim b As Integer
TestDiragain
If Trim(ThisDir) <> "" Then
If Not IsMissing(CreateIt) Then varCreateIt = CBool(CreateIt)
If Len(Dir(ThisDir, 16)) > 0 Then
Retval = True
'does not exist so create it by parsing
ThisDir = NormalizePath(ThisDir)
c = InStr(ThisDir, "\")
If c <= 0 Then b = 1 Else b = c + 2
For i = b To Len(ThisDir)
p = Mid(ThisDir, i, 1)
If p = "\" Then
ThisTest = Left(ThisDir, i - 1)
If Len(Dir(ThisTest, 16)) <= 0 Then
MkDir ThisTest
Next
GoTo TestDiragain
DEout
DirExistCreate = Retval
DEerror
Retval = False
LogError Err, "Unable to create directory " & ThisDir
Resume DEout
Public Function NormalizePath(ThisPath As String) As String
If Right(ThisPath, 1) <> vbNullChar Then
If Right(ThisPath, 1) <> "\" Then
NormalizePath = ThisPath & "\"
NormalizePath = ThisPath
Tip Submitted By: Dick Wilson