Templates Backup Tool

Working on new editor enhancements?
User avatar
HeX0R
Addict
Addict
Posts: 1202
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Templates Backup Tool

Post 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()
Axolotl
Addict
Addict
Posts: 835
Joined: Wed Dec 31, 2008 3:36 pm

Re: Templates Backup Tool

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Templates Backup Tool

Post by Quin »

I never explored the templates at all, thanks for introducing me to them! :D
Cool tool, too 8)
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 280
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Re: Templates Backup Tool

Post by le_magn »

Thanks for this nice tool, added It to my PB...
Image
Post Reply