Date: 02-16-2022
Return to Index
created by gbSnippets
'The PowerBASIC FileCopy statement is all that is needed to implement backup/restore of a file
'Primary Code:
sFileName = "myfile.txt"
FileCopy sFileName, sFileName & ".bak" 'creates "myfile.txt.bak" (overwrites existing .bak file)
FileCopy sFileName & ".bak", sFileName 'restores "myfile.txt.bak to "myfile.txt"
'FileCopy does not support wildcards
'sFilename can contain path (drive/directory) information
'FileCopy overwrites target file - no confirmation asked
'Compilable Example: (Jose Includes)
'This example adds checks to ensure that necessary files exist and
'that the user approves overwriting any existing files.
'Backup/restore are implemented as procedures (Sub)
'In this example, backup files are placed in the same folder as the EXE
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Function PBMain() As Long
CreateTestFiles
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Backup", 50,10,100,20
Control Add Button, hDlg, 101,"Restore", 50,50,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local sfilename As String
sFilename = "myfile.txt"
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then BackupFile (sFileName)
If CB.Msg = %WM_Command AND CB.Ctl = 101 AND CB.Ctlmsg = %BN_Clicked Then RestoreFile (sFileName)
End Function
Sub CreateTestFiles
'creates test files to show how the code works
Open Exe.path$ + "myfile.txt" For Output as #1
Print #1, "main file"
Close #1
Open Exe.path$ + "myfile.txt.bak" For Output as #1
Print #1, "backup file"
Close #1
End Sub
Sub BackupFile (sFileName as String)
'backup
If IsFile(sFileName) Then
'file exists, go ahed with backup
If IsFile(sFileName & ".bak") Then
If MsgBox ("Backup file already exists. Overwrite?", %MB_OkCancel Or %MB_IconQuestion, "Backup File") = %IdOk Then
'user approves overwrite of existing backup file.
FileCopy sFileName, sFileName & ".bak" 'creates "myfile.txt.bak"
Else
'user decided not to overwrite existing backup file. backup cancelled.
MsgBox "Backup Cancelled!", %MB_IconInformation, "Backup File"
End If
Else
'backup file does not exist, go ahead and create
FileCopy sFileName, sFileName & ".bak"
End If
Else
MsgBox "File to copy was not found!", %MB_IconExclamation, "Backup File"
End If
End Sub
Sub RestoreFile (sFileName as String)
'restore
If IsFile(sFileName & ".bak") Then
If MsgBox ("Restore file. Are you sure?", %MB_OkCancel Or %MB_IconQuestion, "Restore File") = %IdOk Then
FileCopy sFileName & ".bak", sFileName 'restores "myfile.txt.bak to "myfile.txt"
Else
MsgBox "Restore cancelled!", %MB_IconInformation, "Restore File"
End If
Else
MsgBox "Backup file not found!", %MB_IconExclamation, "Restore File"
End If
End Sub
'gbs_00140
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm