Date: 02-16-2022
Return to Index
created by gbSnippets
'Here's an implementation of a backup strategy that provides two key
'features.
'1. The user can specify the number of backups.
'2. The backup files may be placed in the location of the original
' file or in "backup" folder below the EXE.extn$
'Primary Code:
'The Backup function declaration include 3 arguments - the full path of the
'file, the maximum numbers of backup copies allowed, and a flag to indicate
'the location where the backup copies will be placed.
Sub Backup (fSource As String, MaxBackups As Long, UseSourceFolder As Long)
'The backup files are names the same as the original, with "_xxxx" added before
'the extension. The _0001 file is always the most recent. Pre-existing backup
'copies are detected and renamed with larger numerical values. When the maximum
'number of backup copies is reached, the oldest copy is deleted.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
Backup "c:\temp\test.bas", 2, 1 '2=max backup 1=use source folder
End If
End Function
Sub Backup (fSource As String, MaxBackups As Long, UseSourceFolder As Long)
On Error GoTo BAF
Local i, iCount As Long, fName, bFolder, bName, bExt, temp As String
iCount = 1 : bName = PathName$(Name, fSource) : bExt = PathName$(Extn, fSource)
'user confirm backup
temp = "The saved version of " + bName + bExt + $CrLf + " will be backed up. Proceed?"
If MsgBox(temp, %MB_OkCancel + %MB_IconInformation, "Backup Active File") = %IdCancel Then Exit Sub
'get target folder/name/extension
bFolder = IIf$(UseSourceFolder&, PathName$(Path, fSource), EXE.Path$ + "backup\")
'get 1st available file name in sequence
Do
fName = bFolder + bName + Format$(iCount, "\_000") + bExt
If IsFile(fName) Then Incr iCount
Loop While IsFile(fName)
'if max count reached, remove that file
If iCount > MaxBackups Then
iCount = MaxBackups
Kill bFolder + bName + Format$(iCount&, "\_000") + bExt
End If
'rename existing files
For i = iCount To 2 Step -1
Name bFolder+bName+Format$(i-1,"\_000")+ bExt As bFolder+bName+Format$(i,"\_000")+bExt
Next i
'always do this
FileCopy fSource, bFolder + bName + "_001" + bExt
Exit Sub
BAF:
MsgBox "Backup error " + Str$(Err), %MB_Ok + %MB_IconExclamation, "Backup Active File"
End Sub
'gbs_00720
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm