Re: temporary filenames
From: Harald Staff (innocent_at_enron.invalid)
Date: 12/15/04
- Next message: Ken Halter: "Re: Experts: Using InterfaceInfoFromObject in a compiled app"
- Previous message: Tom Esh: "Re: Howto ensure application does not get focus"
- In reply to: Hal Tz: "temporary filenames"
- Next in thread: Gaurav - http://www.gauravcreations.com: "RE: temporary filenames"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 15 Dec 2004 15:46:00 +0100
Hi Hal
This is the "proper" way to do it:
Option Explicit
Declare Function GetTempPath Lib "kernel32.dll" _
Alias "GetTempPathA" (ByVal _
nBufferLength As Long, _
ByVal lpBuffer As String) As Long
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
Sub test()
' Generate a temporary file (path)\api????.TMP, where (path)
' is Windows's temporary file directory and ???? is a randomly assigned
unique value.
' Then display the name of the created file on the screen.
Dim temppath As String ' receives name of temporary file path
Dim tempfile As String ' receives name of temporary file
Dim slength As Long ' receives length of string returned for the path
Dim lastfour As Long ' receives hex value of the randomly assigned ????
' Get Windows's temporary file path
temppath = Space(255) ' initialize the buffer to receive the path
slength = GetTempPath(255, temppath) ' read the path name
temppath = Left(temppath, slength) ' extract data from the variable
' Get a uniquely assigned random file
tempfile = Space(255) ' initialize buffer to receive the filename
lastfour = GetTempFileName(temppath, "Yo!", 0, tempfile) ' get a unique
temporary file name
' (Note that the file is also created for you in this case.)
tempfile = Left(tempfile, InStr(tempfile, vbNullChar) - 1) ' extract data
from the variable
MsgBox tempfile
End Sub
I often do a shorter
Sub test2()
tempfile = "Yo!" & Format$(Now, "mmdd_hhmmss") & ".tmp"
MsgBox tempfile
End Sub
Laziness, nothing else.
HTH. Best wishes Harald
"Hal Tz" <HalTz@discussions.microsoft.com> skrev i melding
news:F392BB07-EF42-456B-9A2C-C2CC6C6A004E@microsoft.com...
> anyone know of a way of generating a temporary, unique, filename, as some
C
> runtime library functions do??
> --
> Hal
- Next message: Ken Halter: "Re: Experts: Using InterfaceInfoFromObject in a compiled app"
- Previous message: Tom Esh: "Re: Howto ensure application does not get focus"
- In reply to: Hal Tz: "temporary filenames"
- Next in thread: Gaurav - http://www.gauravcreations.com: "RE: temporary filenames"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|