Re: Check for file open handles

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: C O Jones (umdegraf_at_cc.umanitoba.ca)
Date: 10/27/04


Date: Wed, 27 Oct 2004 18:41:48 -0500

Try this

'
'
' Name:
'
'
'
' isFileOpen.vbs
'
'
'
' Description:
'
'
'
' This function checks to see if the given file is open on the given
server. You may '
' specify any portion of the fully qualified file name, however, you will
want to make '
' sure that whatever you specify is enough to uniquely identify the file
since searching '
' stops on the first matching file. If the file is open, user and path
return the name '
' of the user who has it open, and the fully qualified file name of the
matching file. '
'
'
' Notes:
'
'
'
' With a simple mod you could have the function pass back a dictionary of
ALL files that '
' are open and that match the given file. You could then check to see if
a given file is '
' in that dictionary.
'
'
'
' Usage:
'
'
   '
' result = isFileOpen (server,file,user,path)
'
'
'
' server: the name of a server (eg EMSWEB)
'
' file: part or all of a file name (eg \emsnt1\scd\scd-dir\Top\Paul
MISO\WOLF\07-02 '
' user: the login id of the user who has the file open
'
' path: the fully qualified name of the open file.
'
'
'
' Audit:
'
'
'
' 2004-07-02 jdeg original code
'
'
'

Function isFileOpen ( server , file , user , path )

   dim fso
   dim resource

   isFileOpen = True

   Set fso = GetObject("WinNT://" & server & "/LanmanServer")

   on error resume next

   If (IsEmpty(fso) = False) Then

      For Each resource In fso.resources

         user = resource.User
         path = resource.Path

         if err.Number = 0 then
            If (Not user = "") And (Not Right(user,1) = "$") Then
               if Instr(1,path,file,1) > 0 then exit Function
            End If
         else
            err.Clear
         end if

      Next

   End If

   on error goto 0

   isFileOpen = False

End Function



Relevant Pages

  • Re: VBScript file transfer script
    ... ' stops on the first matching file. ... ' server: the name of a server ... on error goto 0 ...
    (microsoft.public.scripting.vbscript)
  • Re: Check for file open handles
    ... ' stops on the first matching file. ... ' server: the name of a server ... on error goto 0 ...
    (microsoft.public.win32.programmer.wmi)
  • Re: Check for file open handles
    ... > ' stops on the first matching file. ... > on error goto 0 ...
    (microsoft.public.scripting.vbscript)
  • Re: Check for file open handles
    ... > ' stops on the first matching file. ... > on error goto 0 ...
    (microsoft.public.win32.programmer.wmi)
  • Re: FileSystemObject - Method ~ of object ~ failed
    ... > I'm getting having a strange problem trying to move a file from one server ... > Dim fso As New FileSystemObject ... > Run-time error '-2147023766 ' ... Do you get the same problem when you use the FileCopy command? ...
    (microsoft.public.vb.com)