Purebasic "GetTempFileNameWithExtension"

Started by Theo Gottwald, November 10, 2023, 08:10:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

Let's assume you have a command that requires JPEG files, for example. Now you receive a PNG file from your input. What you want to do now is to create a temporary JPEG file to pass it on to a command, for example. Here you can use this command. It gives you a file name, a path and an extension that you want to have in the temp folder. So you get a unique path and file name in the temp folder that you can use to create a temporary file that you can delete later.


ProcedureDLL.s GetTempFileNameWithExtension(Extension.s = "")
  Protected tempPath.s = Space(256)
  Protected tempFileName.s
  Protected final.s
 
  ; Get the temp path
  If GetTempPath_(255, @tempPath)
    tempPath = RTrim(tempPath, Chr(0)) ; Remove trailing null characters
  Else
    ProcedureReturn "" ; Failed to get temp path
  EndIf
 
  ; Get a temporary file name
  tempFileName = Space(256)
  If GetTempFileName_(tempPath, "TMP", 0, @tempFileName)
    tempFileName = RTrim(tempFileName, Chr(0)) ; Remove trailing null characters
  Else
    ProcedureReturn "" ; Failed to get temp file name
  EndIf
 
  ; If an extension is provided, change the file to have that extension
  If Extension <> ""
    final = Left(tempFileName, Len(tempFileName) - 4) + "." + Extension ; Assume the extension from GetTempFileName is .tmp (4 chars)
    RenameFile(tempFileName, final)
  Else
    final = tempFileName
  EndIf
 
  ProcedureReturn final
EndProcedure