So I came up with a way to safely save up to three backups of that file, and to delete older versions of those backups.
Paranoid you might say, but not if you would have to spend hours and hours remaking the data base from scratch.



I've tested this using my file, and it works great.
There are explanation notes in the code below.
But please test using a test file before implementing to ensure there are no bugs.
I'm using PB4.20 latest, on XP Pro.
If there are any questions or suggestions, I can not reply back till later as I have to go to work now.
Hope it's of value to someone else. It's definitely valuable for my needs.
Have a nice day.
Code: Select all
;Note: You will not see any file deleted until there are four files
;created with different _copy_ymd_(YYMMDD).dat dates.
;When first run, it creates a file with that days date.
;You can use copy and paste and create several files and change
;the date to create a test. Elsewise, you won't be able to see it work
;until you save the file on four separate days. Then the oldest
;file will be deleted.
;***************** gets current program path **********************
pp$=Space(#MAX_PATH) ;pp$ = program path
GetCurrentDirectory_(#MAX_PATH,@pp$)
pp$+"\"
;********************************************************************
;**** In your program, enter the file name you want to protect here
;In my example, and in testing this, I'm using
yourfile$="tmr.dat"
;********************************************************************
Procedure FileScan(pp$, yourfile$)
;I find it best to do this at program exit.
;I save the safe copy yourfile$ file with the saved date added to the name.
;eg Date: Aug 15 2008 saving as YYMMDD (ymd) = 080815
;eg: _copy_ymd_(080815).dat is added to file name to distinguish its version from other files.
;A new latest date version file is created when ever you exit.
;This procedure checks for "*).dat" type files
;and deletes all but the last three most recent files at program exit.
;value is checked from left character to right.
;eg: 080803).dat 080809).dat 080815).dat 080807).dat 080803).dat) would be deleted
If ExamineDirectory(1024, pp$, "*).dat")
fnum=0
fn1$=""
fn2$=""
fn3$=""
fn4$=""
Repeat
FileType = NextDirectoryEntry(1024)
FileName.s = DirectoryEntryName(1024)
If FileType = 1
fnum+1
Select fnum
Case 1
fn1$=FileName.s
Case 2
fn2$=FileName.s
Case 3
fn3$=FileName.s
Case 4 To 40 ;note: There should never be more then 4, but just in case during development.
If FindString(FileName.s,"Copy of",1)=0
;protect "Copy of" files, where you use Ctrl c and Ctrl v to copy and paste a file
;in the same directory, in case you want to save a file that you want to keep.
fn4$=FileName.s
EndIf
;move strings so greatest value is from fn1$ to fn4$
If fn1$<fn2$
Swap fn1$, fn2$
EndIf
;at this point we know fn1$>fn2$
;now compare fn2$ with fn3$
If fn2$<fn3$
Swap fn2$, fn3$
EndIf
;now we know fn2$>fn3$
;and we have moved the lowest value to fn3$
;now compare fn3$ with fn4$
If fn3$<fn4$
Swap fn3$, fn4$
EndIf
;now we know fn3$>fn4$
;and we have moved the lowest value to fn4$
;but we need to compare fn1$ with fn2$ again
If fn1$<fn2$
Swap fn1$, fn2$
EndIf
;at this point we know fn1$>fn2$
;now compare fn2$ with fn3$ once more
If fn2$<fn3$
Swap fn2$, fn3$
EndIf
;now just compare fn1$ with fn2$ again to place all values in highest to lowest order
If fn1$<fn2$
Swap fn1$, fn2$
EndIf
DeleteFile(pp$+fn4$) ;this deletes the oldest file of the four files
Debug "fn1$="+fn1$
Debug "fn2$="+fn2$
Debug "fn3$="+fn3$
Debug "fn4$="+fn4$
EndSelect
EndIf
Until FileType = 0
EndIf
FinishDirectory(1024) ;This allows to free the resources associated with the #Directory listing.
EndProcedure
Procedure SaveData(pp$, yourfile$)
;(my programs data file name is tmr.dat , but I cut out the part where my data is saved,
;so you can put your own code here. Just change the file name below to what you want,
;or use yourfile$ to enter your file name above.)
;I Added this next part as a data safety measure in case the program locks up and my yourfile$ file
;is lost some how.
;Just rename yourfile$+"_copy_ymd_(yymmdd).dat" to yourfile$, and you will have the saved data back.
;Saving with date is a much more secure way of saving a back-up of the data base.
;If something happens to the program or data base, it could also transfer the defect to the
;normally saved file above. Using the date method, you will have a good back-up from a
;known previous good file to use, and be able to distinguish latest versions from each file by the
;date being in the name itself.
;A new latest date version file is created when ever you exit.
FileDate$ = FormatDate("%yy%mm%dd", Date())
source$= pp$+yourfile$
dest$= pp$+yourfile$+"_copy_ymd_("+FileDate$+").dat"
CopyFile(source$, dest$)
FileScan(pp$, yourfile$) ;This Procedure keeps the latest three, and deletes older files at program exit.
EndProcedure
;I save program data at program exit.
SaveData(pp$, yourfile$)