Previous Section Table of Contents Next Section

Finding Out Who Has a File Open

graphics/arrow.gif Who Has a File

Listing 31.4 shows the script.

Listing 31.4. WhoHas.vbs. Shows who has a particular file open.

' first, get the server name we want to work with

varServer = InputBox ("Server name to check")



' get the local path of the file to check

varFile= _

 InputBox ("Full path and filename of the file on the" & _

 "server (use the local path as if you were " & _

 "at the server console)")



' bind to the server's file service

set objFS = GetObject("WinNT://" & varServer & _

 "/lanmanserver,fileservice")



' scan through the open resources until we

' locate the file we want

varFoundNone = True



' use a FOR...EACH loop to walk through the

' open resources

For Each objRes in objFS.Resources



      ' does this resource match the one we're looking for?

      If objRes.Path = varFile Then



            ' we found the file - show who's got it

            varFoundNone = False

            WScript.Echo objRes.Path & " is opened by " & _

             objRes.User

      End If

Next



' if we didn't find the file open, display a msg

If varFoundNone = True Then

      WScript.Echo "Didn't find that file opened by anyone."

End If

To operate this script, simply type the name of a server and the full path and filename of a file. This path must be the local path on the server; typing a UNC doesn't work. For example, suppose ServerA has a folder named C:\SalesDocs, which contains a file named Sales.doc. The folder is shared as Sales, and you want to find out who has the file \\ServerA\Sales\Sales.doc open. You'd enter ServerA for the server name, and C:\SalesDocs\Sales.doc as the file path and name.

graphics/arrow.gif Who Has a File-Explained

As with most scripts, this one begins by collecting some basic information: in this case, the name of a server and the complete path and name of a file.


' first, get the server name we want to work with

varServer = InputBox ("Server name to check")



' get the local path of the file to check

varFile= _

 InputBox ("Full path and filename of the file on the" & _

 "server (use the local path as if you were " & _

 "at the server console)")

Next, the script uses ADSI to bind to the specified server's Server service.


' bind to the server's file service

set objFS = GetObject("WinNT://" & varServer & _

 "/lanmanserver,fileservice")

In Listings 31.2 and 31.3, you saw how to iterate through the Server services' shares; this script iterates through open resources instead. First, the script sets a variable indicating that the requested file hasn't yet been found.


' scan through the open resources until we

' locate the file we want

varFoundNone = True

Now the script uses a For Each…Next construct to iterate through the open files.


' use a FOR...EACH loop to walk through the

' open resources

For Each objRes in objFS.Resources

Each resource is checked to see if its path matches the specified file path and filename.


' does this resource match the one we're looking for?

If objRes.Path = varFile Then

If there's a match, the variable is set to False, meaning the file was found. The name of the user who has the file open is displayed in a message box. Notice that the script doesn't use Exit For at this point; more than one user can have a file open, so the script needs to continue looking for other open resources matching the specified file path. There is one resource for each user that has the file open.


            ' we found the file - show who's got it

            varFoundNone = False

            WScript.Echo objRes.Path & " is opened by " & _

             objRes.User

      End If

Next

Finally, the script displays a message if that variable still equals True. This tells you that the script has finished running, but didn't find any open resources matching the file you specified.


' if we didn't find the file open, display a msg

If varFoundNone = True Then

      WScript.Echo "Didn't find that file opened by anyone."

End If

Because this script uses the WinNT ADSI provider, it works with Windows NT 4.0, 2000, XP, and 2003.

    Previous Section Table of Contents Next Section