• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

How to tell if a file is open

With this code:

Open "MyPath.txt" For Input As #3


What code can I use to find out if #3 is still open??

Thanks,

Cheers!
ahammar
0
ahammar
Asked:
ahammar
  • 6
  • 3
  • 3
  • +1
3 Solutions
 
List244Commented:
On Error GoTo 1
Open "MyPath.txt" For Input As #3
Exit Sub
1
''Check here
if err.number = 55 then
   ''ALREADY OPENED error 55 is File already Opened.
end if
0
 
List244Commented:
Basically, you trap for any errors, if one is found you go to the end of the function and check for the error-code 55
or "File already opened" error.

On Error Goto 1 'should be the top of your code
''YOUR CODE HERE
Exit Sub
1
if Err.Number = 55 Then
    'ERROR IS FILE ALREADY OPENED
    'HANDLE HERE
End If ''Should go at the end.  In between these should be your normal function.
0
 
ahammarAuthor Commented:
Thanks List244,

I already knew that, but I was just curious if there was a way for VB to tell if it had a file open without checking for error number.
I will give you the points if no-one knows or if this can't be done since you were first, but I'm still curious.

Thanks again,
Cheers!
ahammar
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
List244Commented:
Ahammar, I do not believe there is a way to check for a file opened without trapping an error.  To what I am aware
programs do not keep track of their opened files in a way that they can close them.  Of course, you yourself could keep
track of what you open and close to know what is opened at any given time.
0
 
leclairmCommented:
I don't have vb on my computer at this moment as I'm on site for the week.  Just an idea though, what if you try this:

dim x as integer
x = freefile

Whenever you open a file in your app, use the above as the filenumber.  

Like I said, I can't test this but what happens if you open a file using x (open yourfile for input as # x
), then dimension another variable as integer and assign it freefile.  What is the value of the integer at this point.

If freefile is sequential, it will definitely not be one and you will know that another file is using it.

I might be wrong, but it's worth trying.
0
 
List244Commented:
That, would work if you were sure your file structures were sequential.  However, the problem arises when one function
opens #3 and another opens number.  When closing say #1 closes properly, but #3 does not.  While #3 is opened, the
next freeFile is still number 1.
0
 
List244Commented:
Is your goal simply to close the files?  If you just want to close the opened files, and you know a range, you could do:

Dim i as Integer
For i = 0 To 100
   Close #i ''No error will occur closing already closed files.
Next i
0
 
nffvrxqgrcfqvvcCommented:
If you are going open files like that then any process can open and share the file. This would cause conflict with checking if the file is open or not depending if another application locks it. You should open your file for Lock access this way no other process can access it.


Option Explicit

Public Function ReadFile(FileName As String) As String

    Dim b()     As Byte
    Dim f       As Long
   
    f = FreeFile
   
    Open FileName For Binary Access Read Write Lock Read Write As #f
    ReDim b(LOF(f) - 1)
    Get #f, , b
    ReadFile = StrConv(b, vbUnicode)
    Close #f
   
    Erase b
   
End Function

Private Sub Command1_Click()
   
    Debug.Print Len(ReadFile("c:\pn.txt"))

End Sub
0
 
nffvrxqgrcfqvvcCommented:
I made a mistake...use this instead should be fast

Option Explicit

Public Enum ReadType
    Binary = &H1
    Anscii = &H2
End Enum

Public Function ReadFile(FileName As String, AccessMode As ReadType) As String

    Dim b()     As Byte
    Dim f       As Long
   
    f = FreeFile
   
    Open FileName For Binary Access Read Write Lock Read Write As #f
    ReDim b(LOF(f) - 1)
    Get #f, , b
    Select Case AccessMode
        Case Anscii
        ReadFile = StrConv(b, vbUnicode)
        Case Binary
        ReadFile = b
    End Select
    Close #f
    Erase b
   
End Function

Private Sub Command1_Click()
   
    Debug.Print ReadFile("c:\windows\notepad.exe", Binary)

End Sub
0
 
ahammarAuthor Commented:
Thanks everybody,

I think the answer is that there is no way.  I just wanted to know that if a file was open for input as #3, then could vb tell me that it had #3 open.  I will just split the points to everyone who helped out here.  It wasn't a big deal, it was just something I was curious about.  I do appreciate all the help here today.  Thanks very much!

Cheers!
ahammar
0
 
List244Commented:
Well, sorry couldn't help more, but I also think the answer is that it can not be done.
0
 
ahammarAuthor Commented:
I gave List244 125 points, leclairm 60 points, and egl1044 65 points

List 244 was first and I think also correct in stating that VB can't do that.
leclairm gave me a way that could possibly be added to and arranged so that it could be done as a workaround
and egl1044 gave me code to open a file and locking it...something new to me.

Thanks to all!
ahammar
0
 
nffvrxqgrcfqvvcCommented:
If you want to use api then you can examine my code for returning a filename from a filehandle, you could edit it a bit to add the handles to a collection and check if the handle is open if the handle is open it will return the file name it would work with some tweaking however you won't be able to use the VB open method you need to use createfile API.

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=63062&lngWId=1
0
 
leclairmCommented:
Thanks for the points and sorry we couldn't figure out a workaround.

@list244:

<..However, the problem arises when one function opens #3 and another opens number..>

Were you able to test this??  I'm curious because like I said I'm unable to use vb at the moment so I can't test it and I've never tried to see how FreeFile allocates numbers.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now