Random File Name
In a couple of instances we've had a need to create a temporary file on the local file system (either a client machine or the server). The temporary file needs to be a file that doesn't exist on the file system (we don't want to overwrite something that exists). So we ended up writing a little function to create a random file name that doesn't exist on the local system. To use this function, pass in the starting directory and the file extension. A good way to get the starting directory is to find out the Notes Data directory from the notes.ini file using this statement:
directory = session.GetEnvironmentString("Directory", True)
You will be told the data directory (in either the client or server environment) and can use that to pass into the function. Here is the function:
Function RandomFileName(inpDir As String, ext As String) As String
' Pass in the name of a directory, build a random file name, make sure the file does not exist
' in the directory, and then return the full path (directory + file name) to the new file name.
Dim directory As String
Dim slash As String
Dim checkFileName As String
Dim fileName As String
Dim i As Integer
Dim num As Integer
Dim ch As String
directory = inpDir ' We don't want to change the passed-in parm, so make a local copy of the value
If Instr(directory, "/") = 0 And Instr(directory, "\") = 0 Then
RandomFileName = "" ' No slashes. Unknown file system setup - return empty string
Exit Function ' ======== EXIT ============
End If
If Instr(directory, "/") <> 0 And Instr(directory, "\") <> 0 Then
RandomFileName = "" ' Both slashes. Unknown file system setup - return empty string
Exit Function ' ======== EXIT ============
End If
If Instr(directory, "/") <> 0 Then slash = "/" Else slash = "\"
' Make sure the directory exists first
If Right(directory, 1) <> slash Then directory = directory & slash ' Format dir so it ends in a slash
On Error Resume Next
checkFileName = Dir$(directory & "*.*") ' Look for something in the directory
On Error Goto 0
If Err <> 0 Then ' Will give a "path not found" error if the path doesn't exist
Err = 0
RandomFileName = ""
Exit Function ' ======== EXIT ============
End If
Randomize
fileName = "" ' Get us into the while loop
While fileName = ""
For i = 1 To 8 ' Random file name will have 8 characters
If i = 1 Then num = Rnd * 52 Else num = Rnd * 62 ' starts w/ a letter, but other chars can be nums
Select Case num
Case 0 To 25 : fileName = fileName & Chr$(Asc("A")+num) ' 0...25 = upper case letter
Case 26 To 51 : fileName = fileName & Chr$(Asc("a")+num-26) ' 26...52 = lower case letter
Case 52 To 61 : fileName = fileName & Cstr(num-52) ' 52...61 = number
End Select
Next
checkFileName = Dir$(directory & fileName & "." & ext) ' See if the file exists
If checkFileName <> "" Then fileName = "" ' If found, continue the while loop
Wend
' At this point, the random file name doesn't exist in the dir - return full path to the new file
RandomFileName = directory & fileName & "." & ext
End Function
The function returns a random file name that does not exist in that directory, or an empty string if there was some kind of an error.