GetTempFileName Function

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

Platforms: Win 32s, Win 95/98, Win NT

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 is 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.

lpszPath
The path to put the temporary file into. This is preferably the same path gotten from GetTempPath.
lpPrefixString
The first three characters of the filename.
wUnique
If non-zero, this number's last four digits in hexidecimal are the last four characters in the filename, and the file is not created. If zero, the last four characters are generated by Windows, and the file is created.
lpTempFileName
A string that receives the path and filename of the temporary file.

Example:

' Generate a temporary file (path)\api????.TMP, where (path)
' is Windows's temporary file directory and ???? is a randomly assigned unique value.
' Then display the name of the created file on the screen.
Dim temppath As String  ' receives name of temporary file path
Dim tempfile As String  ' receives name of temporary file
Dim slength As Long  ' receives length of string returned for the path
Dim lastfour As Long  ' receives hex value of the randomly assigned ????

' Get Windows's temporary file path
temppath = Space(255)  ' initialize the buffer to receive the path
slength = GetTempPath(255, temppath)  ' read the path name
temppath = Left(temppath, slength)  ' extract data from the variable

' Get a uniquely assigned random file
tempfile = Space(255)  ' initialize buffer to receive the filename
lastfour = GetTempFileName(temppath, "api", 0, tempfile)  ' get a unique temporary file name
' (Note that the file is also created for you in this case.)
tempfile = Left(tempfile, InStr(tempfile, vbNullChar) - 1)  ' extract data from the variable
Debug.Print "Temporary filename: "; tempfile

See Also: GetTempPath
Category: Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Go back to the Windows API Guide home page.
E-mail: rogue953@st-louis.crosswinds.net
This page is at http://www.vbapi.com/ref/g/gettempfilename.html