Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Sample Function to Generate a Random Temporary File Name


View products that this article applies to.

Summary

This article shows you how to create a sample function that generates a random file name that can be used for a temporary file.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

↑ Back to the top


More information

Sometimes, an application has to create a file that it uses temporarily and then deletes. The names of temporary files are important and you do not want your application to inadvertently overwrite an existing file.

In such cases, you can create and use the user-defined function called MakeTempFileName(). This function accepts a file extension and returns a string representing a non-existent, random file name with the file extension that you specify. The file name includes a path to your Windows Temp folder (directory) so that the temporary file is placed in the same folder that Windows uses to write its temporary files. This is handy for those who perform periodic maintenance on their hard disks and delete any leftover temporary files in the Windows Temp folder.

To create the sample function MakeTempFileName(), follow these steps:
  1. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
  2. Type the following procedure:
    Function MakeTempFileName(Extension As String) As String
    
       Dim Isfile As Integer, FHandle As Integer, Cntr As Integer
       Dim WinTemp As String, TF As String
    
       Isfile = False
       FHandle = FreeFile
    
       WinTemp = Environ("TEMP") & "\"
       For Cntr = 1 To 8
          WinTemp = WinTemp & Mid(LTrim(Str(CInt(Rnd * 10))), 1, 1)
       Next Cntr
    
       TF = Trim(WinTemp$) & "." & Extension
    
       Open TF For Output As #Fhandle
       Debug.Print TF
       Print #FHandle, "This is a Temp file"
         
       Close #FHandle
       MakeTempFileName = TF
    
    End Function
  3. Type the following line in the Immediate window and press ENTER:
    TempFileName$ = MakeTempFileName("TMP")
    Note that the name of the temporary file is displayed in the Immediate window. The file contains the line "This is a Temp file." After you have verified that the file exists in your Temp folder, you can delete it.

↑ Back to the top


Keywords: KB210602, kbprogramming, kbinfo

↑ Back to the top

Article Info
Article ID : 210602
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 354