Date: 02-16-2022
Return to Index
created by gbSnippets
'The PowerBASIC FileAttr function can tell if a file has been opened. This test is
'used to prevent an error when attempting to re-open a file that is already open.
'Primary Code:
result& = FileAttr(#1, 0) '%True-open %False-not open
'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
Open Exe.path$ + "myfile.txt" For output As #1 'remove to see what happens if file is not open
If FileAttr(#1,0) Then
MsgBox "myfile.txt is already open." 'do not attempt to open the file
Else
MsgBox "myfile.txt is closed"
Open "myfile.txt" For Input as #1 'knowing the file is closed, proceed with opening it
Close #1
End If
Close #1
End If
End Function
'gbs_00145
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm