Visual Basic File API Routines
GetTempFileName: Create Temporary Files for Application Use
     
Posted:   Sunday August 2, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

Occasions may arise when its advantageous to use temporary files for manipulation of data during a program's execution. One method is to create your own files in the directory of your choosing; another is to use the built-in Windows function GetTempFileName to do the work for you. This page demonstrates using this in conjunction with the GetTempPath API to create application-specific temporary files in the system's \Temp folder.

On a successful call to GetTempFileName, an empty file is created in the path specified. The file is not open, therefore you are free to use either VB's or Windows' file manipulation methods against the file. The file is not deleted automatically when your app terminates .. the cleanup is left up to you.

Don't be deceived by appearances below.  You will find that once in your app with the comments trimmed down that the actual code to call this function is quite compact.

 BAS Module Code
None.

 Form Code
Add a list (List1), and two command buttons (Command1, Command2) to a form, along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function GetTempPath _
    Lib "kernel32" Alias "GetTempPathA" _
   (ByVal nBufferLength As Long, _
    ByVal lpBuffer As String) As Long

Private 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

Private Const MAX_PATH As Long = 260



Private Sub Form_Load()

   Command1.Caption = "Create Temp File"
   Command2.Caption = "Quit"
   List1.Clear
   
End Sub


Private Sub Command1_Click()

  '--- demo only ---
  'display the created file
   List1.AddItem "file = " & vbTab & CreateTempFilename()
   List1.AddItem ""
  '----------------- 
   
  'typical actual calling syntax ...
  'sTempFile = CreateTempFilename()

End Sub


Private Sub Command2_Click()

   Unload Me
   
End Sub


Private Function GetSystemTempPath() As String
   
   Dim result As Long
   Dim buff As String
   
  'get the user's \temp folder
  'pad the passed string
   buff = Space$(MAX_PATH)
   
   result = GetTempPath(MAX_PATH, buff)
   
  'result contains the number of chrs up to the
  'terminating null, so a simple left$ can
  'be used. Its also conveniently terminated
  'with a slash.
   GetSystemTempPath = Left$(buff, result)

End Function


Private Function CreateTempFilename() As String
   
  'Note: Calling GetTempFileName() *will create*
  'an empty temporary file at the location passed.
  'As coded, this demo does not clean up the files
  'it creates; naturally your well behaved app will.
  
  'The first param in GetTempFileName is the path
  'in which to create the file. This is not restricted
  'to the system temporary files folder - any folder
  'can be specified. Passing "." creates the file in
  'the current directory. The GetSystemTempPath() call
  'used below calls Windows' GetTempPath() API to
  'returns the system's temp folder.
  
  'The second param is the prefix string (note:
  'null-terminated under NT). GetTempFileName() uses
  'only up to the first three characters of this string
  'as the prefix of the filename, as all temp files
  'created are in the 8.3 format. The string passed
  'in this parameter must consist of characters in
  'the ANSI character set.
  
  'The third parameter, uUnique, specifies an
  'unsigned integer that the function converts to
  'a hexadecimal string for use in creating the
  'temporary filename. If uUnique is nonzero, the
  'function appends the hexadecimal string to
  'lpPrefixString to form the temporary filename.
  'In this case, the function does not create the
  'specified file, and does not test whether the
  'filename is unique.

  'If uUnique is zero, as below, the function uses
  'a hexadecimal string derived from the current
  'system time. In this case, the function uses
  'different values until it finds a unique filename,
  'and then it creates the file in the lpPathName directory.
  
  'The last param is the variable to contain the temporary
  'filename, null-terminated and consisting of characters
  'in the ANSI character set. This string should be padded
  'at least the length, in bytes, specified by MAX_PATH to
  'accommodate the path.

   Dim result As Long
   Dim buff As String
   
  'Pad a working string & call the API.
  'If the function succeeds, the return contains
  'the unique numeric value (in decimal) used
  'in the temporary filename. If the function 
  'fails the return value is zero.
   buff = Space$(MAX_PATH)
   result = GetTempFileName(GetSystemTempPath(), "vbn", 0, buff)

   If result <> 0 Then
      
     'strip the trailing null
      CreateTempFilename = Left$(buff, InStr(buff, Chr$(0)) - 1)
      
     '--- demo only ---
     'display the decimal and hex value in the list
      List1.AddItem "result = " & vbTab & result & vbTab & Hex(result)
     '-----------------
     
   End If

End Function
 Comments
Pressing the Create button will create a temporary file in the specified folder. Remember that it's the application's responsibility - not Windows' - to delete any temporary files it creates.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter