VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Functions | Generate A Temporary File Name

This function returns a temporary file name in the current directory or in a specified directory.

The following code is needed in the General|Declarations area of the module.

__________________________________________________________________________________________

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

__________________________________________________________________________________________

...and now the routine....
__________________________________________________________________________________________

Public Function GetTempFileName(Optional ByVal sPrefixString As String = "~TM", _
                                Optional ByVal sPath As String = "") As String
    Dim nRet As Long
    Dim lpTempFileName As String

    If sPath = "" Then
        sPath = CurDir$
    End If
    lpTempFileName = Space$(1024)
    nRet = GetTempFileNameAPI(sPath, sPrefixString, 0, lpTempFileName)
    If nRet = 0 Then
        GetTempFileName = ""
    Else
        GetTempFileName = Trim$(lpTempFileName)
    End If
End Function

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr