Source code for Issue Number 78

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just create a new Module and then paste this source code into it. You should name the class basFileSystem.

'----------------------------------------------------------------------
'
'   Module Name:    basFileSystem
'   Written By:     C&D Programming Corp.
'   Create Date:    2/99
'   Copyright:      Copyright 1999 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

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 Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
        (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH = 260


' Windows 95/98: The GetTempPath function gets the temporary file path as follows:
'
'   The path specified by the TMP environment variable.
'   The path specified by the TEMP environment variable, if TMP is not defined or if
'       TMP specifies a directory that does not exist.
'   The current directory, if both TMP and TEMP are not defined or specify nonexistent
'       directories.

' Windows NT: The GetTempPath function does not verify that the directory specified
'       by the TMP or TEMP environment variables exists. The function gets the
'       temporary file path as follows:
'
'   The path specified by the TMP environment variable.
'   The path specified by the TEMP environment variable, if TMP is not defined.
'   The Windows directory, if both TMP and TEMP are not defined

Public Function TempPath() As String
    Dim sTemp As String
    Dim lRet As Long
    Dim lLen As Long

    On Error GoTo Handler
    sTemp = Space$(MAX_PATH)
    lLen = GetTempPath(MAX_PATH, sTemp)
    TempPath = Left(sTemp, lLen)
    Exit Function

Handler:
    Err.Raise Err.Number, "TempPath", Err.Description
End Function


'   Attempts to form a unique filename based on the sPrefix and 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. TempFileName then creates a file by that name and closes it.

Public Function TempFile(Optional sPrefix As String = "VB_", Optional sTempFileLocation As String = "") As String
    Dim sTempFile As String
    Dim lLen As Long
    Dim iNull As Integer

    On Error GoTo Handler
    If sTempFileLocation = "" Then
        sTempFileLocation = TempPath
    End If
    sTempFile = Space$(MAX_PATH)
    lLen = GetTempFileName(sTempFileLocation, sPrefix, 0, sTempFile)

    ' find null and trim everything after it.
    iNull = InStr(sTempFile, vbNullChar)
    If iNull = 0 Then
        TempFile = ""
    Else
        TempFile = Left(sTempFile, iNull - 1)
    End If
    Exit Function

Handler:
    Err.Raise Err.Number, "TempFile", Err.Description
End Function