Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating filenames 3

Status
Not open for further replies.

aclayborne

Programmer
May 3, 2000
49
US
I'm looking for a way to generate new filenames. In FoxPro there is a sys function that generates unique filenames. Does VB have anything equivilant or do I have to write a function to do this.

Thanks
Acie Clayborne
 
Example :
Generate temporary files. The following code choose file names that not exist in your temp directory, to avoid collision with other files. It will pop up the file name that been chosen.

Module Code
Public 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

Form Code

Private Sub Form_Load()
'replace all the "c:\temp" below with your temp directory
MsgBox GenTempName("c:\temp")
End Sub

Private Function GenTempName(sPath As String)
Dim sPrefix As String
Dim lUnique As Long
Dim sTempFileName As String

If IsEmpty(sPath) Then sPath = "c:\temp"
sPrefix = "fVB"
lUnique = 0

sTempFileName = Space$(100)
GetTempFileName sPath, sPrefix, lUnique, sTempFileName
sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1)
GenTempName = sTempFileName
End Function


Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
This works...

'Module level[tt]
Declare Function GetTempFileName Lib _
"kernel32.dll" Alias "GetTempFileNameA" _
(ByVal lpszPath As String, _
ByVal lpPrefixString As String, _
ByVal wUnique As Long, _
ByVal lpTempFileName As String) As Long
[/tt]

'Call it like this...[tt]
YourPath$ = "C:\"
YourFile$ = Space(255)
YourPrefix$ = "Huh" [/tt]
'3-character prefix for the file
' If the second to last parameter is zero then the last 4 digits
' in the file name are randomly generated by Windows and the file
' is automatically created, otherwise,
' if it's a hexadecimal number between 0000 and FFFF,
' it will be the rightmost four digits of the filename (in hex)
' and the file is not created[tt]
lastfour = GetTempFileName(YourPath$, YourPrefix$, 0, YourFile$)
MsgBox "Unique filename: " & YourFile$
[/tt]

Unfortunately, Windows automatically appends a TMP extention, which shouldn't be too hard to parse out with the Left$ function.
VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
Sorry Eric, I was composing while you were posting. I never would have hit the Submit button if I had known!
VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
Although the FileSystemObject does invoke mixed feelings, it offers an easy answer to this particular problem:

Dim objFso as FileSystemObject
Dim strFileName as String

Set objFso = New FileSystemObject
strFileName = objFso.GetTempName

Set objFso = Nothing

strFileName contains a DOS 8.3 format filename ending in .tmp. As Alt255 pointed out, this shouldn't be too hard to change.

PS. The FileSystemObject can be added to your project through Project .>. References.>.Scripting Runtime Library (scrrun.dll). _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
I like that. For those of us who distrust the FileSystemObject and hate the API, the task can be performed using "straight" VB code:
[tt]
MyDir$ = "C:\Test\"
MyExt$ = ".txt"
Do
MyFile$ = ""
For Re = 1 To 8
MyFile$ = MyFile$ & Chr$(Int(26 * Rnd + 65))
Next
MyFile$ = MyDir$ & MyFile$ & MyExt$
If Dir(MyFile$) = "" Then Exit Do
Loop
MsgBox MyFile$ & " does not exist yet."
[/tt]

Unlike the API call, this won't overwrite a pre-existing file.
VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
rvBasic -

Could you please combine all three methods posted here (API, FileSystemObject, and straight VB code), and post them as a FAQ under the "Files" section?

Thanks!
Chip H.
 
Storing existing information in a FAQ can save everybody a lot of time. Good idea, Chip!


VCA.gif

Alt255@Vorpalcom.Intranets.com

"If you can get people to ask the wrong questions, they'll never find the right answers."[tt]
Thomas Pynchon[/tt]

Perhaps the reverse is also true....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top