Date: 02-16-2022
Return to Index
created by gbSnippets
'It's common operation to ask a user for permission to overwrite
'an existing file. This function verifies that the file exists and
'asks the user for permission to overwrite.
'Primary Code:
Function ConfirmOverWrite (sFileName$) as Long '-1 approval, 0 not approved
If IsFile(sFileName$) Then
'file exists. ask user for permission to overwrite
Select Case MsgBox ("File exists. Overwrite?", %MB_okcancel Or %MB_IconQuestion, "Save")
Case %IDOK
Function = %True
Case %IDCancel
Function = %False
End Select
Else
Function = %True 'if file does not exist, permission is granted to create it
End If
End Function
'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
Local sFileName$
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
sFileName = Exe.path$ + "myfile.txt"
If ConfirmOverWrite(sFileName$) Then
Kill sFileName$ 'optional - useful for working with binary files
Open sFileName$ For Output As #1
Print #1, "Overwritten"
Close #1
End If
End If
End Function
Function ConfirmOverWrite (sFileName$) as Long '-1 approval, 0 not approved
If IsFile(sFileName$) Then
'file exists. ask user for permission to overwrite
Select Case MsgBox ("File exists. Overwrite?", %MB_okcancel Or %MB_IconQuestion, "Save")
Case %IDOK
Function = %True
Case %IDCancel
Function = %False
End Select
Else
Function = %True 'if file does not exist, permission is granted to create it
End If
End Function
'gbs_00142
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm