Re: temporary filenames

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Harald Staff (innocent_at_enron.invalid)
Date: 12/15/04


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



Relevant Pages

  • Re: Where to write scrap files?
    ... string and then use the PRNG to pick the digits from 0-9 4 times. ... The tmpfile function creates a temporary binary file that is ... removed when it is closed or at program termination. ... whether an open temporary file is removed is ...
    (comp.sys.acorn.programmer)
  • Re: getting a string as the return value from a system command
    ... I want to call a system command that returns a string, ... I could always create a temporary file, ... module with the temporary file as the stdout argument, ... "I have come to believe that the whole world is an enigma, a harmless enigma ...
    (comp.lang.python)
  • Re: C99 IDE for windows
    ... guessing wrong is that the resulting string is truncated, ... either accept that or try again with a bigger buffer. ... guessing wrong is undefined behavior. ... representation to a temporary file and, at the same time, find out how ...
    (comp.lang.c)
  • Re: FILE * friom C to ruby with swig
    ... This function a FILE * to a temporary file it fill with user data, ... I don't know exactly how to deal with FILE* - there may be a standard SWIG typemap to deal with this, or you might search for examples to convert it to a Ruby File object. ... contain a string value that the function copy to it from an other variable ...
    (comp.lang.ruby)