Results 1 to 3 of 3

Thread: Create folder in temp directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    11

    Create folder in temp directory

    Considered the following code:
    Code:
    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
    
    Public Function GetTempDir() As String
        Dim sBuffer As String
        Dim lRetVal As Long
    
        sBuffer = String(255, vbNullChar)
    
        lRetVal = GetTempPath(Len(sBuffer), sBuffer)
    
        If lRetVal Then
            GetTempDir = Left$(sBuffer, lRetVal)
        End If
    End Function
    
    Private Sub Command1_Click()
    Dim sBuffer As String
     sBuffer = String$(255, vbNullChar)
    GetTempFileName GetTempDir(), "aaa", 0, sBuffer
    MsgBox sBuffer
    
    End Sub
    My question is:
    1)Does the code above guarantee to generate a unique filename?
    2)This code can only generate file with .tmp extension in user temp directory, how to create a folder with unique name?
    Last edited by Hack; Jul 10th, 2007 at 09:37 AM. Reason: Added [code] [/code] tags

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Create folder in temp directory

    Easiest would be to keep generating temp file names with the .tmp extension to a string, stripping the .tmp and adding whatever extension you like, then checking to see if the new filename exists. Lather, rinse, repeat until you find a unique one.

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Create folder in temp directory

    If the function fails, the return value is zero.

    Do Until GetTempPath(Len(sBuffer), sBuffer)
    Loop
    'adjust sBuffer for Chr(0)
    MkDir sBuffer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width