Visual Basic Code of the Week (COTW)
Issue #78
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Deal of the Week

Get your copy of NetMon, the Internet Performance Monitor for only $10. Download a free evaluation copy of NetMon at

Bonus Issue Update

We have updated issue number 23 (Parsing words/tokens using any delimiter) to support multi-character delimiters. If you are a paid subscriber, download the updated source code at

In this Issue

In this issue we discuss how to get the current temporary path or a unique temporary filename using several API calls.

This source code is designed for VB 5.0 and up. It can be used with VB 4.0 32-bit by changing the Optional declarations. Questions? Email us at


The basFileSystem module includes two functions: TempPath and TempFile. TempPath retrieves the Windows temporary directory and TempFile retrieves a unique temporary filename.


Public Function TempPath() As String

TempPath retrieves the directory where most temporary files are stored while using Windows 95/98/NT. Under Windows 95/98 this function will retrieve the temporary path as follows:

Under Windows NT it will retrieve the temporary path as follows (NOTE: The TempPath function does not verify that the directory specified by the TMP or TEMP environment variables exists):


Public Function TempFile(Optional sPrefix As String = "VB_", Optional sTempFileLocation As String = "") As String

TempFile attempts to form a filename using the Prefix you supplied and a unique number based on the current system time. If a file with the resulting file name exists, the number is increased by one and the test for existence is repeated. Testing continues until a unique file name is found. TempFile then creates a file by that name and closes it.

The format of the filename created will be pppnnnn.tmp and it will be created by default in the standard Windows temporary directory as returned from TempPath. ppp is the value specified in sPrefix and nnnn is a unique number based on the current system time.

Sample Usage

The below sample describes how to use the TempPath and TempFile functions.

    MsgBox "Windows Temporary Directory: " & TempPath & vbCr & _
            "Sample Temporary Filename: " & TempFile & vbCr & _
            "Temp File with a custom prefix: " & TempFile("CTW")

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945