|
-
Jul 10th, 2007, 09:32 AM
#1
Thread Starter
New Member
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
-
Jul 10th, 2007, 10:08 AM
#2
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.
-
Jul 10th, 2007, 10:18 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|