Results 1 to 19 of 19

Thread: checking file is open**resolved**

  1. #1

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195

    Question 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.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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

  3. #3

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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

  4. #4
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442
    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

  5. #5

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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.

  6. #6

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    anyone? please jst tell me if it is possible then tell how and if not jst say no its not possible. thank

  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Ok...

    VB Code:
    1. 1
    2. On error goto ErrSub:
    3. open...
    4. close...
    5.  
    6. msgbox "Opened"
    7. exit sub
    8.  
    9. ErrSub:
    10. Err.Clear
    11. DoEvents ' so u don't freeze?
    12. Goto 1

    Good luck,

  8. #8

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    still giving me an error. it give me the error and i have no time to close the file.

    well maybe its not possible

  9. #9
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Are you using freefile?

  10. #10

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    no im not i dont need to i dont think.

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    What's the error you're receiving?

  12. #12

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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.

  13. #13

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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

  14. #14
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    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:
    1. if FreeFile = 1 then
    2.       'File is not open currently
    3. Else
    4.       'File is already open
    5. end if

    Of course this isnt the "normal" function of FreeFile, but you could take advantage of it in this way.

  15. #15

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    will that work if i open it in two completely different programs

  16. #16
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    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:
    1. if FreeFile = 1 then
    2.       'File is not open currently
    3. Else
    4.       'File is already open
    5. 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 ...

  17. #17

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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.

  18. #18
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    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:
    1. Public Function FileIsOpen(strFileName) As Boolean
    2.     Dim i As Integer
    3.     On Error GoTo errtrap
    4.     FileIsOpen = False
    5.     i = FreeFile
    6.     Open strFileName For Input As #1
    7.     Close #1
    8.     Exit Function
    9. errtrap:
    10.     FileIsOpen = True
    11. End Function

    usage:

    If FileIsOpen("C:\Test.txt") then
    msgbox "it is open"
    end if

  19. #19

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    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
  •  



Click Here to Expand Forum to Full Width