如何确定文件是否已使用VBS锁定? [英] How can I determine if a file is locked using VBS?
问题描述
我正在编写一个VB脚本来更新网络上的某些文件.在开始之前,我想知道是否有任何文件被锁定.我想在实际执行任何更新之前 进行此操作.
I am writing a VB Script to update some files on the network. Before beginning, I want to know if any of the files are locked. I'd like to do this before I actually do any updates.
我知道如果尝试替换文件时文件被锁定,我可以处理该错误,但是我真的很想知道在开始更新任何文件之前是否文件已被锁定.
I am aware that I can handle the error if the file is locked when I try to replace it, but I really want to know if any files are locked before I start updating any files.
是否有任何方法可以查看文件是否已使用VBS锁定(除了尝试替换文件以外)?
Is there any way to see that a file is locked using VBS (apart from trying to replace it)?
推荐答案
此函数确定是否可以在写入"模式下访问感兴趣的文件.这与确定文件是否被进程锁定并不完全相同.不过,您可能会发现它适合您的情况. (至少直到出现更好的情况为止.)
This function determines whether a file of interest can be accessed in 'write' mode. This is not exactly the same as determining whether a file is locked by a process. Still, you may find that it works for your situation. (At least until something better comes along.)
此功能将指示文件被另一个进程锁定时无法进行写"访问.但是,它无法将该条件与其他阻止写"访问的条件区分开.例如,如果文件设置了只读位或具有限制性的NTFS权限,则写"访问也是不可能的.当进行写"访问尝试时,所有这些条件都将导致权限被拒绝".
This function will indicate that 'write' access is not possible when a file is locked by another process. However, it cannot distinguish that condition from other conditions that prevent 'write' access. For instance, 'write' access is also not possible if a file has its read-only bit set or possesses restrictive NTFS permissions. All of these conditions will result in 'permission denied' when a 'write' access attempt is made.
还请注意,如果文件被另一个进程锁定,则此函数返回的答案仅在函数执行时才是可靠的.因此,并发问题是可能的.
Also note that if a file is locked by another process, the answer returned by this function is reliable only at the moment the function is executed. So, concurrency problems are possible.
如果发现以下任何一种情况,则会引发异常:未找到文件",未找到路径"或非法文件名"(错误的文件名或编号").
An exception is thrown if any of these conditions are found: 'file not found', 'path not found', or 'illegal file name' ('bad file name or number').
Function IsWriteAccessible(sFilePath)
' Strategy: Attempt to open the specified file in 'append' mode.
' Does not appear to change the 'modified' date on the file.
' Works with binary files as well as text files.
' Only 'ForAppending' is needed here. Define these constants
' outside of this function if you need them elsewhere in
' your source file.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
IsWriteAccessible = False
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Dim nErr : nErr = 0
Dim sDesc : sDesc = ""
Dim oFile : Set oFile = oFso.OpenTextFile(sFilePath, ForAppending)
If Err.Number = 0 Then
oFile.Close
If Err Then
nErr = Err.Number
sDesc = Err.Description
Else
IsWriteAccessible = True
End if
Else
Select Case Err.Number
Case 70
' Permission denied because:
' - file is open by another process
' - read-only bit is set on file, *or*
' - NTFS Access Control List settings (ACLs) on file
' prevents access
Case Else
' 52 - Bad file name or number
' 53 - File not found
' 76 - Path not found
nErr = Err.Number
sDesc = Err.Description
End Select
End If
' The following two statements are superfluous. The VB6 garbage
' collector will free 'oFile' and 'oFso' when this function completes
' and they go out of scope. See Eric Lippert's article for more:
' http://blogs.msdn.com/b/ericlippert/archive/2004/04/28/when-are-you-required-to-set-objects-to-nothing.aspx
'Set oFile = Nothing
'Set oFso = Nothing
On Error GoTo 0
If nErr Then
Err.Raise nErr, , sDesc
End If
End Function
这篇关于如何确定文件是否已使用VBS锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!