Home All Groups Group Topic Archive Search About

Vb6 to check if a text file is already opened

Author
13 Feb 2007 1:05 PM
Pradeep
Hello,

I need to determine if a text file is already opened or not.

Per the below KB article i wrote a method but it does not work for a text
file. It works for an excel file though (thats what the KB says of course
:-)).

http://support.microsoft.com/kb/213383

Public Function CheckIfFileIsOpen(strFileName As String) As Boolean
On Error GoTo ErrPoint

Dim lngFileNum As Long, lngErrNum As Long

lngErrNum = 0

lngFileNum = FreeFile

Open strFileName For Input Lock Read As #lngFileNum

If lngErrNum = 70 Then
   CheckIfFileIsOpen = True
   Exit Function
End If

Close #lngFileNum

         CheckIfFileIsOpen = False

            Exit Function
ErrPoint:
         If Err.Number = 70 Then
            lngErrNum = Err.Number
            Resume Next
         End If
End Function

I need to determine if a text file is already open using VB6.

Any help in this regard is highly appreciated.

Cheers,

Pradeep

Author
13 Feb 2007 2:22 PM
MikeD
Show quote Hide quote
"Pradeep" <Prad***@discussions.microsoft.com> wrote in message
news:ECD54F1A-9168-4AD3-A94E-8BFEB60CF95C@microsoft.com...
> Hello,
>
> I need to determine if a text file is already opened or not.
>
> Per the below KB article i wrote a method but it does not work for a text
> file. It works for an excel file though (thats what the KB says of course
> :-)).
>
> http://support.microsoft.com/kb/213383
>
> Public Function CheckIfFileIsOpen(strFileName As String) As Boolean
> On Error GoTo ErrPoint
>
> Dim lngFileNum As Long, lngErrNum As Long
>
> lngErrNum = 0
>
> lngFileNum = FreeFile
>
> Open strFileName For Input Lock Read As #lngFileNum
>
> If lngErrNum = 70 Then
>   CheckIfFileIsOpen = True
>   Exit Function
> End If
>
> Close #lngFileNum
>
>         CheckIfFileIsOpen = False
>
>            Exit Function
> ErrPoint:
>         If Err.Number = 70 Then
>            lngErrNum = Err.Number
>            Resume Next
>         End If
> End Function
>
> I need to determine if a text file is already open using VB6.
>
> Any help in this regard is highly appreciated.


You won't be able to write anything that will be 100% reliable with text
files. The reason is that many applications, when opening text files, simply
read the file into memory and then close it. So, technically, the file's not
open even though you might be working with it.  Notepad is like this.

--
Mike
Microsoft MVP Visual Basic
Author
14 Feb 2007 4:37 PM
Dave O.
"MikeD" <nob***@nowhere.edu> wrote in message
news:%23UMHlp3THHA.600@TK2MSFTNGP05.phx.gbl...
> You won't be able to write anything that will be 100% reliable with text
> files. The reason is that many applications, when opening text files,
> simply read the file into memory and then close it. So, technically, the
> file's not open even though you might be working with it.  Notepad is like
> this.
>
> --
> Mike
> Microsoft MVP Visual Basic

Notepad is holding "something", try this; create a folder, make a text file
in the folder, open it with Notepad.
While Notepad is open you can delete the file with no problem, but you
cannot delete the folder until you close Notepad.
Important? No
Interesting? No
Don't know why I bothered, but there you are :-)

Dave O.
Author
2 Mar 2007 11:41 AM
Pradeep
Hello Dave,

I am sorry about the delay in responding.

Your suggestion has worked.

Thanks a lot.

Also, sincere thanks to everyone who bothered to drop a suggestion.

Cheers,

Pradeep