Get your copy of NetMon, the Internet Performance Monitor for only $10. Download a free evaluation copy of NetMon at http://www.codeoftheweek.com/netmon/index.html
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 http://www.codeoftheweek.com/membersonly/bi/0023.html
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 firstname.lastname@example.org.
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.
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")
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: http://www.codeoftheweek.com/membersonly/bi/0078.html