|
Post by angelo2449 on Dec 21, 2022 5:45:11 GMT -5
Hi guys,
Is it possible to know if a File is open before closing it and causing an error?
Maybe using lof() or eof()?
Thank you.
|
|
|
Post by Rod on Dec 21, 2022 6:56:56 GMT -5
The traditional way is just to set a flag on opening and check that at the end to be sure to close it.
|
|
|
Post by xxgeek on Dec 21, 2022 15:11:37 GMT -5
The traditional way is just to set a flag on opening and check that at the end to be sure to close it. Also, if the file is closed elsewhere in the program, don't forget to reset the flag. Here's an example open "file.txt" for input as #1 : fileTXTOpen = 1 blah blah 'when closing do a check before closing, and reset the flag. if fileTXTOpen = 1 then close #1 : fileTXTOpen = 0
|
|
|
Post by Brandon Parker on Dec 22, 2022 14:49:52 GMT -5
If you are looking for a method to use that does not include keeping track of files that are open using flags, here is how I do it. I am a big fan of this method, and it also works for other handle types as well.
Global False : False = 0 Global True : True = 1
Open "Your file path here" For Input As #Test
Print safeClose("#Test") Print Print safeClose("#Test") End
Function safeClose(handle$) On Error GoTo [Error] Close #handle$ safeClose = True Exit Function [Error] safeClose = False End Function
{:0)
Brandon Parker
|
|