Subscribe to Windows IT Pro
April 22, 2002 12:00 AM

Determine Who Has a File Open

Windows IT Pro
InstantDoc ID #24503
Rating: (3)
Downloads
24503.zip

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 r2r@winnetmag.com. 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://
<servername>/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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Jun 28, 2005

    is there anyway this code can be adapted to identify the computer name of resource.User?

  • JBaum
    8 years ago
    Oct 13, 2004

    Thanks! There seem to be VERY few vbscript solutions that address this need. This is extremely helpful.

  • Jim Parker
    9 years ago
    Nov 21, 2003

    Like your artical however, we are not running ADSI and it would take a large effort I am told to install it. Is there any other way to get the name of who has file open? We are constantly faced with this problem. I know we can determine that the file is open by way of the error number, but it's the who that's the problem. Calling around is a waste of time trying to find out who has it open. We use VB6 for the majority of our programming. Any help is greatly appreciated.

  • James Turner
    10 years ago
    Oct 30, 2002

    For my environment (with a lot of open files and shared folders), using the automated program is much easier!

  • Jeff Rausch
    10 years ago
    Oct 30, 2002

    James Turner's Reader to Reader: "Determine Who Has a File Open" (May 2002, InstantDoc ID 24503) provides a Visual Basic (VB) script for getting user and path information. Here's an easy way to access the information if you're using Windows NT or Windows 9x:



    1. Run winfile.exe (in \\%systemroot%system32).

    2. Browse to and select the file you want*even across a network share.

    3. Press Alt+Enter to view that file's properties.

    4. Click Open by to determine who has the file open. (You can even close connections for that file.)


    5. If you've upgraded to Windows 2000 from NT or Win9x, Winfile will still work. Win2K's Shared Folders Microsoft Management Console (MMC) add-in performs a similar role but only over shares.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.