Generate Temporary Files

Generate temporary files. The following code choose file names that not exist in your temp directory, to avoid collision with other files. It will pop up the file name that been chosen.

Module Code

Public Declare Function GetTempFileName Lib "kernel32" _
     Alias "GetTempFileNameA" (ByVal lpszPath As String, _
     ByVal lpPrefixString As String, ByVal wUnique As Long, _
     ByVal lpTempFileName As String) As Long

Form Code

Private Sub Form_Load()
'replace all the "c:\temp" below with your temp directory
MsgBox GenTempName("c:\temp")
End Sub

Private Function GenTempName(sPath As String)
Dim sPrefix As String
Dim lUnique As Long
Dim sTempFileName As String

If IsEmpty(sPath) Then sPath = "c:\temp"
sPrefix = "fVB"
lUnique = 0

sTempFileName = Space$(100)
GetTempFileName sPath, sPrefix, lUnique, sTempFileName
sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1)
GenTempName = sTempFileName
End Function

Go Back