Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check file lock

Posted on 2003-11-09
3
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

721 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