Page 1 of 1

Templates Backup Tool

Posted: Sun Apr 06, 2025 5:53 pm
by HeX0R
I didn't touch PB for a while and was going to create a small tool today.
When opening my templates, I was quite shocked, it was simply E.M.P.T.Y.!
So ~100 templates gone, nice!
I read about that problem in the past (seems to happen rarely only) and always thought "pff.. guess it was the users fault, that will never happen to me".
Well.. here we are now, quite frustrated, I found a three year old backup at least, better than nothing.

Anyway, long story short, it was time for a quick backup IDE tool for the Templates:

Code: Select all

; [IDE Tool]
; Template Backup
; (c) HeX0R 2025
;
; Compile, add as PB IDE Tool, give it some name, no Arguments and let it be triggered by "Editor closing"
; at first start it will ask for a backup path
; whenever Templates.prefs size has changed, it will add a backup there.
; they will be named (keep it simple):
; Templates_001.prefs
; Templates_002.prefs
; ...


Procedure DoBackup(Template$, BackupPath$)
	Protected i, Num.i = 1
	
	While FileSize(BackupPath$ + "Templates_" + RSet(Str(Num), 3, "0") + ".prefs") > -1
		Num + 1
	Wend
	CopyFile(Template$, BackupPath$ + "Templates_" + RSet(Str(Num), 3, "0") + ".prefs")
EndProcedure
	

Procedure MakeSureDirectoryExists(Dir$)
	Protected Count, i, CurrDir$
	
	If Right(Dir$, 1) <> #PS$
		Dir$ + #PS$
	EndIf
	Count = CountString(Dir$, #PS$)
	For i = 1 To Count
		CurrDir$ + StringField(Dir$, i, #PS$) + #PS$
		If FileSize(CurrDir$) <> -2
			CreateDirectory(CurrDir$)
		EndIf
	Next i
EndProcedure

Procedure main()
	Protected Path$, Template$, Own$
	Protected Template_Size, BackupPath$
	
	Path$ = GetPathPart(GetEnvironmentVariable("PB_TOOL_Preferences"))
	Own$  = GetUserDirectory(#PB_Directory_ProgramData) + "hex0r" + #PS$ + "IDE_bk" + #PS$
	MakeSureDirectoryExists(Own$)
	OpenPreferences(Own$ + "bk.prefs")
	Template_Size = ReadPreferenceInteger("LastSize", -1)
	BackupPath$   = ReadPreferenceString("BackupPath", "")
	ClosePreferences()
	
	If BackupPath$ = ""
		;first run
		BackupPath$ = PathRequester("Select path to store template backups to", "")
		If BackupPath$ = ""
			MessageRequester("Quit", "Can't go on without knowing the backup path, closing now!")
			ProcedureReturn
		Else
			CreatePreferences(Own$ + "bk.prefs")
			WritePreferenceString("BackupPath", BackupPath$)
			ClosePreferences()
		EndIf
	EndIf

	If Path$ = ""
		MessageRequester("Info", "I need to be started as PB IDE tool, closing now!")
		ProcedureReturn 
	EndIf
	Template$ = Path$ + "Templates.prefs"
	If FileSize(Template$) <> Template_Size
		Template_Size = FileSize(Template$)
		DoBackup(Template$, BackupPath$)
		OpenPreferences(Own$ + "bk.prefs")
		WritePreferenceInteger("LastSize", Template_Size)
		ClosePreferences()
	EndIf
	
EndProcedure

main()

Re: Templates Backup Tool

Posted: Tue Apr 08, 2025 4:23 pm
by Axolotl
Nice. Thanks for sharing.
To be honest, I don't really use the templates very much, but your post has reminded me that I should clean them up a bit.

Re: Templates Backup Tool

Posted: Tue Apr 08, 2025 5:16 pm
by Quin
I never explored the templates at all, thanks for introducing me to them! :D
Cool tool, too 8)

Re: Templates Backup Tool

Posted: Wed Apr 09, 2025 10:47 am
by le_magn
Thanks for this nice tool, added It to my PB...