Windows: CreateTemporaryFileName
Posted: Wed Jul 28, 2004 11:24 pm
Not much past the Win32 API, but takes away all the nasty hassle of doing this yourself or embedding lots of crap inline into your code. See the description in the function header - there's a demo below it.
PS, I searched here (and the Code Archive) first but could only find some discussion of GetTempFileName_(), but nothing showing a simple way to use it, so apologies if this already exists somewhere.
Code: Select all
; Name: CreateTemporaryFileName
; Synopsis: filename = CreateTemporaryFileName(prefix)
; Parameters: prefix.s - The prefix to be applied to the temporary filename
; Returns: filename.s - A filename in the temporary directory, with a temporary name
; Globals: None
; Description: Creates a filename for a temporary file, which will be in the format of
; <temp_dir>\<prefix><uuuu>.TMP where temp_dir is the temporary directory (see
; below), prefix is up to 3 characters from the passed prefix string and
; uuuu is a unique identifier generated by the OS.
;
; <temp_dir> calculation (taken from MSDN docs for GetTempPath
; The GetTempPath function does not verify that the directory specified by the TMP Or TEMP environment variables exists. The function gets the temporary file path as follows:
; The path specified by the TMP environment variable.
; The path specified by the TEMP environment variable, If TMP is not defined.
; The Windows directory, If both TMP And TEMP are not defined.
; Windows Me/98/95: The GetTempPath function gets the temporary file path as follows:
; The path specified by the TMP environment variable.
; The path specified by the TEMP environment variable, If TMP is not defined Or If TMP specifies a directory that does not exist.
; The current directory, If both TMP And TEMP are not defined Or specify nonexistent directories.
Procedure.s CreateTemporaryFileName(prefix.s)
DefType.s filename
DefType.l temp_path_length ; Length of the string in the temporary path
Dim temp_path.w(0) ; Array of words (string of TCHARs) to hold the temporary pathname, if required
Dim temp_file.w(MAX_PATH + 1) ; Array of words (string of TCHARs) to hold the temporary filename, if required
Dim prefix_oem.w(4) ; TCHAR string for the prefix string if any
; Set return filename to empty (to indicate failure of some sort)
filename = ""
; Get required length of temporary directory
temp_path_length = GetTempPath_(0, @temp_path(0)) - 1 ; GTP_() includes terminating NULL, remove that
If temp_path_length And temp_path_length <= (#MAX_PATH-14)
; Create TCHAR string for the number of characters required + NULL and get string
Dim temp_path.w(temp_path_length + 1)
If temp_path_length = GetTempPath_(temp_path_length + 1, @temp_path(0))
; Try to create the prefix 3 characters
OemToCharBuff_(@prefix, @prefix_oem(0), 3)
; Get temporary filename in entirety
If GetTempFileName_(@temp_path(0), @prefix_oem(0), 0, @temp_file(0))
; Convert TCHAR string to ASCII, for use with PB
filename = Space(temp_path_length)
If CharToOem_(@temp_file(0), @filename)=0
filename = ""
EndIf ; Failed to convert the TCHAR string to ASCII
EndIf ; Created a temporary filename
EndIf ; Temporary path copied successfully
EndIf ; Temporary path had a valid length
ProcedureReturn filename
EndProcedure
tfn.s = CreateTemporaryFileName("foo")
If tfn<>"" And CreateFile(0, tfn)
WriteStringN("Hello, I am a temporary file")
CloseFile(0)
Else
MessageRequester("Gack!", "Could not create temporary file", #PB_MessageRequester_OK)
EndIf
End