Temp file generator

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Temp file generator

Post by Trond »

Finds a new filename suitable for a temporary file.

Code: Select all

#APPCODE = "myapp"

Procedure.s NewTempFilename()
  Protected Temp.s = GetTemporaryDirectory()
  Protected C.l
  Protected Result.s
  For C = 0 To 255
    Result = #APPCODE + Str(ElapsedMilliseconds()) + Str(C) + ".tmp"
    If FileSize(Result) = -1
      ProcedureReturn Result
    EndIf
  Next
EndProcedure

Procedure FinishTempFilename(Filename.s)
  ProcedureReturn DeleteFile(Filename)
EndProcedure
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Temp file generator

Post by c4s »

A little side note... You get but aren't using the temporary directory so better use the following:
Correct code wrote:Result = Temp + #APPCODE + Str(ElapsedMilliseconds()) + Str(C) + ".tmp"
Better 29 month later than never ;)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply