Solved

Check file lock

Posted on 2003-11-09
3
318 Views
Last Modified: 2010-05-01
How could one check to see if a file is in use by another user. Example: I need to delete a file but before I do, check to see if it’s in use.

Is this possible?

   - Marc
0
Comment
Question by:MarcGraff
3 Comments
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 250 total points
ID: 9710753
try this,

Private Sub Form_Load()
Dim strFileName As String
' Full path and name of file.
strFileName = "C:\calc.exe"
' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, file in not in use
MsgBox "not open, can delete it"
Else
MsgBox "file is open, cant delete it"
End If
End Sub

Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
'MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 9711324
0
 
LVL 1

Author Comment

by:MarcGraff
ID: 9711914
Thank you so much!

   - Marc
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question