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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking to see if a file is open

Status
Not open for further replies.

impulse24

IS-IT--Management
Jul 13, 2001
167
US
Hi,

How can I check to see if a file is open with vb code. I am currently moving files from one directory to another in code, but if a file is open I want to skip it. What's the best way to do this?
 
I guess my answer is a question really !

Do you not merely try to open the file, if it can't be opened then it must be open !!!

??????
Is this one way of checking ?

OLAPer
 
Nope..What I am doing is moving files from one directory to another. There is another program that at any point will be in the process of writing data to one of the files. With my current program I am able to move the file without generating an error, the other program simply creates a new file, and starts writing to it. I want to avoid moving the file, as now half the data resides in one file, and the other half resides in another(the file is a text file by the way)
 
Try opening the file in exclusive mode prior to the move.

Code:
Private Function CanOpen (filename as String) as Boolean
  Dim f as Long
  f = Freefile
  On Error Resume Next
  Open filename for Input Access Read Lock Read as f
  If Not Err
    CanOpen = True
  Else
    CanOpen = False
    Err.Clear
  End If
End Function

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top