|
-
Jun 11th, 2003, 01:17 PM
#1
Thread Starter
Addicted Member
checking file is open**resolved**
Hey everyone how are you doing.
Anyway how would i tell if a text file is already open. like if i have one program that write to the file and another program that does to. how would i be able to tell if it is open.
thanks for the help
Last edited by big_k105; Jun 12th, 2003 at 10:58 AM.
-
Jun 11th, 2003, 01:20 PM
#2
well for one thing the second program would get an error trying to open it if its already open.. so if you trap the error using error handling.. that is one way you could tell
-
Jun 11th, 2003, 01:21 PM
#3
Thread Starter
Addicted Member
yeah that is the way i have it but i want to make the 2nd program to keep trying till it can open it. and i couldnt get that to work with trapping the error. i was hoping maybe there was another way to do it
and thanks for the reply
-
Jun 11th, 2003, 01:35 PM
#4
Hyperactive Member
If u open that Document in Notepad for example, u can't see if the document is open... Notepad reads the Textfile in the memory and then directly closes the file again.
There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me
-
Jun 11th, 2003, 01:37 PM
#5
Thread Starter
Addicted Member
actually the program that is opening it jst has 2 buttons one to open and one to close this program is to test the other program. i want to be able to detect when another vbprogram has the file open and the second one wants to write to the text file.
-
Jun 11th, 2003, 02:38 PM
#6
Thread Starter
Addicted Member
anyone? please jst tell me if it is possible then tell how and if not jst say no its not possible. thank
-
Jun 11th, 2003, 03:17 PM
#7
So Unbanned
Ok...
VB Code:
1
On error goto ErrSub:
open...
close...
msgbox "Opened"
exit sub
ErrSub:
Err.Clear
DoEvents ' so u don't freeze?
Goto 1
Good luck,
-
Jun 11th, 2003, 03:28 PM
#8
Thread Starter
Addicted Member
still giving me an error. it give me the error and i have no time to close the file.
well maybe its not possible
-
Jun 11th, 2003, 03:30 PM
#9
So Unbanned
-
Jun 11th, 2003, 03:32 PM
#10
Thread Starter
Addicted Member
no im not i dont need to i dont think.
-
Jun 11th, 2003, 03:42 PM
#11
So Unbanned
What's the error you're receiving?
-
Jun 11th, 2003, 03:45 PM
#12
Thread Starter
Addicted Member
Run-time Error 70:
Permission Denied
and i know it cause the file is already open too cause i have another program that is running and it has the file open. and i got all the permissions set to work for me cause that was a problem this morning.
-
Jun 12th, 2003, 09:26 AM
#13
Thread Starter
Addicted Member
Is there no way to check if a file is open. if there is please tell me if not then jst say "no there isnt". and i will drop this question. and try something else. thanks
-
Jun 12th, 2003, 09:34 AM
#14
PowerPoster
Originally posted by big_k105
Is there no way to check if a file is open. if there is please tell me if not then jst say "no there isnt". and i will drop this question. and try something else. thanks
When you open the file in either place, open it as #1.
then you can always tell if it is currently open with:
VB Code:
if FreeFile = 1 then
'File is not open currently
Else
'File is already open
end if
Of course this isnt the "normal" function of FreeFile, but you could take advantage of it in this way.
-
Jun 12th, 2003, 09:36 AM
#15
Thread Starter
Addicted Member
will that work if i open it in two completely different programs
-
Jun 12th, 2003, 09:36 AM
#16
PowerPoster
Originally posted by Muddy
When you open the file in either place, open it as #1.
then you can always tell if it is currently open with:
VB Code:
if FreeFile = 1 then
'File is not open currently
Else
'File is already open
end if
Of course this isnt the "normal" function of FreeFile, but you could take advantage of it in this way.
sorry ... scratch that ... I just realized you said it is a second app that has it open ...
-
Jun 12th, 2003, 09:38 AM
#17
Thread Starter
Addicted Member
ok, im i stuck using "On Error". i hope not but thats what its looking like.
and if i am stucking using that is there a way to specify to do something on a specified error and then do something else on a different error. if they are in the same sub.
-
Jun 12th, 2003, 09:49 AM
#18
PowerPoster
Originally posted by big_k105
ok, im i stuck using "On Error". i hope not but thats what its looking like.
and if i am stucking using that is there a way to specify to do something on a specified error and then do something else on a different error. if they are in the same sub.
I think trapping an error is the only way. Why not reduce it to an intuitive function though? ie
VB Code:
Public Function FileIsOpen(strFileName) As Boolean
Dim i As Integer
On Error GoTo errtrap
FileIsOpen = False
i = FreeFile
Open strFileName For Input As #1
Close #1
Exit Function
errtrap:
FileIsOpen = True
End Function
usage:
If FileIsOpen("C:\Test.txt") then
msgbox "it is open"
end if
-
Jun 12th, 2003, 10:58 AM
#19
Thread Starter
Addicted Member
thanks alot muddy. that worked great on the forum load.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|