Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if file open

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
0
0
US
How do you check if a file is open?

We had a program that may or may not have opened a file already and I need to see if the file is already opened. If I don't, I get an error saying file is already open.

Thanks,

Tom.
 
I do not know how to check if the file is already open, but you could use error handling to trap the error.

Code:
Private Sub openfile()
On Error GoTo errorhandler

'Code

Exit Sub
errorhandler:
'If error = file already open then do whatever

end sub
 
But how would you know that the error was from an open file problem?

Thanks,

Tom.
 
What kind of file is it?

the technique a lot of programs (including MS Office Apps, SolidWorks, Etc..) use is to create a temporary file with a ~prefix such as

if the original was:
file.txt

the temp file would be:
~file.txt

then say:
if Dir("~file.txt") = "" then
...Open the file, Blah Blah, Close, etc...
End If

Otherwise, you could use the On Error commands to trap the error and skip to the part after the File Operation..

Code:
Function FileOpen(File as string) As Boolean
  FileOpen = True
  On Error Goto OpenFailed
  If Dir(File)<>"" Then
    Open File for input as #1
    Close
    FileOpen = False
  End If
OpenFailed:
End Function

This Function will return:
True if the File is Not Open and Does Exist
False if the File is Open or Does Not Exist

I think there is a function like freefile, or something, you may want to use in place of #1, but that is the general structure to use...

the best thing to do is to Open a file, load the file to memory, then close the file asap to avoid these types of problems, you can the view/modify the file from the memory buffer as you like, then save it back to the file...

This code Opens a file, loads it, changes the characters to upper case, then saves it back to the file.
Code:
Dim TextBuffer as string
Open "File.txt" For Input as #1
  TextBuffer = Input(LOF(1),#1)
Close

TextBuffer = UCase(TextBuffer)

Open "File.txt" For Output As #1
  Print #1, TextBuffer
Close

Good Luck,
Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Update:

Yes it is FreeFile...

Such as:
Code:
Function FileOpen(File as string) As Boolean
  On Error Goto OpenFailed
  FileOpen = True
  If Dir(File)<>"" Then
    Dim FileNum As Integer
    FileNum = FreeFile
    Open File for Input as FileNum
    Close
    FileOpen = False
  End If
OpenFailed:
End Function

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top