TmpFile

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

TmpFile

Post by AZJIO »

I looked at how it was done in AutoIt3 and did the same on PureBasic.

Code: Select all

EnableExplicit

Procedure.s TmpFile(sDirName$ = "", Prefix$ = "~", Ext$ = ".tmp", RandomLength = 7)
	Protected TmpName$
	
	If RandomLength <= 0 Or RandomLength > 130
		RandomLength = 7
	EndIf
	If Not Asc(sDirName$) Or FileSize(sDirName$) = -1
		sDirName$ = GetTemporaryDirectory()
; 		If FileSize(sDirName$) = -1
; 			sDirName$ = GetPathPart(ProgramFilename())
; 		EndIf
	EndIf
	
	If Not CheckFilename(Prefix$)
		Prefix$ = "~"
	EndIf
	If Not CheckFilename(Ext$)
		Ext$ = ".tmp"
	EndIf

	If Right(sDirName$, 1) <> #PS$
		sDirName$ + #PS$
	EndIf
	If Asc(Ext$) And Left(Ext$, 1) <> "."
		Ext$ = "." + Ext$
	EndIf

	; Create the temporary file path without writing to the selected directory
	Repeat
		; Create a random filename
		TmpName$ = ""
		While Len(TmpName$) < RandomLength
			TmpName$ + Chr(Random(122, 97))
		Wend
		; Temporary filepath
		TmpName$ = sDirName$ + Prefix$ + TmpName$ + Ext$
	Until FileSize(TmpName$) = -1 ; End the loop if no file with the same name is present
	ProcedureReturn TmpName$
EndProcedure

Debug TmpFile()
Simplified code

Code: Select all

Procedure.s TmpFile(sDirName$ = "")
	Protected TmpName$

	If Not Asc(sDirName$) Or FileSize(sDirName$) = -1
		sDirName$ = GetTemporaryDirectory()
	EndIf

	If Right(sDirName$, 1) <> #PS$
		sDirName$ + #PS$
	EndIf

	Repeat
		TmpName$ = ""
		While Len(TmpName$) < 7
			TmpName$ + Chr(Random(122, 97))
			TmpName$ + Chr(Random(90, 65))
			TmpName$ + Chr(Random(57, 48))
		Wend
		TmpName$ = sDirName$ + TmpName$
	Until FileSize(TmpName$) = -1
	ProcedureReturn TmpName$
EndProcedure

Debug TmpFile()
more

Code: Select all

Procedure.s TmpFile(DirName$ = "")
	Protected TmpName$, *mem, i, *a.Ascii

	If Not Asc(DirName$) Or FileSize(DirName$) = -1
		DirName$ = GetTemporaryDirectory()
	EndIf

	If Right(DirName$, 1) <> #PS$
		DirName$ + #PS$
	EndIf

	*mem = AllocateMemory(7, #PB_Memory_NoClear)
	If *mem
		Repeat
			*a = *mem
			For i = 1 To 7
				Select Random(2)
					Case 0
						*a\a = Random(122, 97)
					Case 1
						*a\a = Random(90, 65)
					Case 2
						*a\a = Random(57, 48)
				EndSelect
				*a + SizeOf(Ascii)
			Next
			TmpName$ = DirName$ + PeekS(*mem, 7, #PB_Ascii)
		Until FileSize(TmpName$) = -1
		FreeMemory(*mem)
	EndIf

	ProcedureReturn TmpName$
EndProcedure

Debug TmpFile()
Looked at this function which uses the time in the filename, but it's only for Windows.

Code: Select all

Debug GetTemporaryDirectory() + "~" + Hex(Random(9223372036854775807)) + ".tmp"
Debug GetTemporaryDirectory() + "~" + Hex(Date()) + Hex(Random(2147483647)) + ".tmp"
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: TmpFile

Post by BarryG »

Pro Tip: Don't ever use GetTemporaryDirectory() for temp files; use your own temp directory in your app's install folder. Reason: Some of my users were reporting that my app wasn't working properly, and it turns out they used a PC cleaning program to clear the %TEMP% folder from time to time (where my app's temp files were). So now I just use my own temp folder to be 100% safe. Until I knew they were doing this, it was a bug I could never resolve, because I had no idea they were manually deleting files that were needed. To be fair, they didn't know they were deleting needed files, either.
Bitblazer
Enthusiast
Enthusiast
Posts: 761
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: TmpFile

Post by Bitblazer »

BarryG wrote: Sat Mar 04, 2023 8:21 am Pro Tip: Don't ever use GetTemporaryDirectory() for temp files; use your own temp directory in your app's install folder. Reason: Some of my users were reporting that my app wasn't working properly, and it turns out they used a PC cleaning program to clear the %TEMP% folder from time to time (where my app's temp files were). So now I just use my own temp folder to be 100% safe.
For windows:

Code: Select all

TempFolderName$ = GetEnvironmentVariable("LocalAppData") + "\Temp"
Debug "Temp = '" + TempFolderName$ + "'"
LocalAppData or AppData is the environment variables name for the folder in windows. Keep in mind that the folder is used by many other apps too.
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: TmpFile

Post by useful »

@BarryG
The implication is that temporary files are alive as long as the application that created them is alive and cannot be deleted because they are open. Before terminating the application, they must be deleted accordingly.
This is what I thought for the last 45 years :)
Dawn will come inevitably.
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: TmpFile

Post by BarryG »

@BitBlazer: Your snippet returns the same folder as GetTemporaryDirectory(), which is %TEMP%.

@useful: I know what you mean, but the reality is that temp files are not always necessarily in use (such as "undo" files). Basically, since %TEMP% is a shared folder and not limited to just your app, it's too risky and unsafe to assume that your temp files will always be there.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: TmpFile

Post by mk-soft »

You can use the Temp folder for files.
These are safe as long as they are open. You have to lock your own subfolders in the Temp folder until they are no longer needed.
However, you should also delete the files and folders you have created yourself.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: TmpFile

Post by luis »

useful wrote: Sat Mar 04, 2023 9:20 am This is what I thought for the last 45 years :)
And you were right :P
"Have you tried turning it off and on again ?"
A little PureBasic review
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: TmpFile

Post by BarryG »

I agreed that he is right - temp files should be locked, but that doesn't stop people using cleaning apps to force-delete them, which was my experience. You can't stop people messing with their system when they think they know best.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: TmpFile

Post by AZJIO »

@BarryG

For program files I use %APPDATA%\MyProgName

Code: Select all

;- ini
PathConfig$ = GetPathPart(ProgramFilename())
; PathConfig$ = GetPathPart(ProgramFilename()) + "MyProgName" + #PS$
If FileSize(PathConfig$ + "MyProgName.ini") = -1
	CompilerSelect #PB_Compiler_OS
		CompilerCase #PB_OS_Windows
			PathConfig$ = GetHomeDirectory() + "AppData\Roaming\MyProgName\"
		CompilerCase #PB_OS_Linux
			PathConfig$ = GetHomeDirectory() + ".config/MyProgName/"
; 		CompilerCase #PB_OS_MacOS
; 			PathConfig$ = GetHomeDirectory() + ".config/MyProgName/"
; 			PathConfig$ = GetHomeDirectory() + "Library/Application Support/MyProgName/"
	CompilerEndSelect
EndIf
ini$ = PathConfig$ + "MyProgName.ini"
A temporary file is convenient for testing, if you need to check the results of writing to a file, immediately open it and see.
In a real program, I also output to a file, for example, a list of found files, if I don’t want to force the user to go through the gestures of saving the file and then force him to delete this file looking for where it was saved. The result opens in his notebook by default, and he himself decides whether this data is important, whether it should be saved to another file, or whether a new request for a list of files with different parameters should be made. In any case, he loses his data, not the program. I hope he sees the file path in the title of his notebook. If the file opened on its own, then the user himself understands that he is not given control over the file and must be saved if he needs it.

yes, I also sometimes clear the folder, especially on someone else's computer, where there are several gigabytes of data and a breeding ground for a virus.
CleanTemp.cmd

Code: Select all

cd /d "%temp%" && rd /s /q "."
beware of running if the %temp% folder is not defined for you, it will delete everything in a row. I warned.
Post Reply