Declare Function GetTempFileName Lib "kernel32.dll" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
GetTempFileName generates a filename for a temporary file and optionally creates it for you. Temporary files are used to store data for short periods of time on the hard drive. The full filename, including the path, is put into the string variable passed as lpTempFileName. The format of the generated filename path\xxxuuuu.TMP. Path is the path to put the file in, preferably the temporary directory supplied by Windows, which can be gotten through GetTempPath. xxx is the string specified in lpPrefixString. uuuu is a hexadecimal number between 0000 and FFFF. If wUnique is non-zero, uuuu is the rightmost four digits of the number (in hexadecimal), and the file is not created. This method will work even if a file with that name already exists. If wUnique is zero, uuuu is a random number generated by Windows, and the file is created. Temporary files always have a .TMP extension. The function returns the value used for uuuu if successful, or 0 if an error occured.
Example:
' Display a randomly generated temporary filename
Dim temppathx As String * 255, tempfilex As String * 255
x = GetTempPath(255, temppath) ' get Windows's temp directory
temppath = Left(temppathx, x) ' extract data from string
x = GetTempFileName(temppath, "API", 0, tempfilex)
' Extract the filename from the string
tempfile = Left(Trim(tempfilex), Len(Trim(tempfilex)) - 1)
Debug.Print "Temp filename is: " tempfile
' Filename is in format (path)\API????.TMP
Related Call: GetTempPath
Category: Files
Back to the index.