Determine Who Has a File Open

A reader uses a Visual Basic script rather than Server Manager to find out which users have a particular file open.

Readers

April 21, 2002

2 Min Read
ITPro Today logo in a gray background | ITPro Today


Editor's Note: Share your Windows and .NET discoveries, comments, problems, solutions, and experiences with products and reach out to other Windows & .NET Magazine readers (including Microsoft). Email your contributions (400 words or less) to [email protected]. Please include your phone number. We edit submissions for style, grammar, and length. If we print your submission, you'll get $100. Submissions and listings are available online at http://www.winnetmag.com/articles. Enter the InstantDoc ID number in the InstantDoc ID text box.

A few months ago, several members of our development staff came to me with a problem. One of their files was locked, and no one could access it; they needed to know who had the file open.

I launched Server Manager, double-clicked the server that housed the file, and clicked In Use. I had to painstakingly scroll through more than 500 open resources to find the file. Two users had the file open. I looked up the users' IDs and gave the development staff the users' names.

Two days later, two members of the development staff came to me with the same problem. Finding the open file took even longer this time. To minimize the hassle should the situation occur again, I wrote the Visual Basic (VB) script that Listing 1 shows. If you're running Windows NT, this script requires Active Directory Service Interfaces (ADSI) 2.5.

The script lets you enter a filename or part of a filename and returns user and path information for files that match your input. The script outputs to a Microsoft Excel spreadsheet so that you can sort by user or file if necessary. (You might find the script's code to create, open, and populate an Excel spreadsheet useful for your other scripts.) The script's power resides in the LanmanServer service operation, which the following line of code implements:

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

The servername variable is the server that houses the file you're looking for. The script contains code that allows for user input; the Instr() function in this code eliminates case sensitivity. If you don't know the exact name of the file you're looking for, you can just enter a few letters of the filename. The statement

If (Not resource.User = "") And (Not Right(resource.User,1) = "$")

lets you bypass machine connections that don't have user information.

—James Turner
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like