\[Editor's Note: Email your scripting solutions (400 words or less) to Reader to Reader at [email protected] Please include your script and phone number. We edit submissions for style, grammar, and length. If we print your contribution, you receive $100.\]
Recently, some developers flocked around my desk. One of their files was locked, and no one could access it. They needed to know who had the file open. I clicked Start, Programs, Administrative Tools, Server Manager; double-clicked the server on which the file was stored; and clicked In Use. I had to scroll through more than 500 open resources to find the desired file. Then, I looked up the ID numbers of the two users who had the file open, found their names, and gave the names to the developers.
I could have used File Manager (winfile.exe) to find the file, but that technique is also quite laborious if users know only the filename and the server on which the file resides. Most users map to shares or are automatically mapped from a logon script and don't know the full path to the file. In these cases, you must open Server Manager, look up the share (if users know the name of it), find the full path, run Winfile, traverse the folders (which could be many-layered), select the file, click Properties, and click Used By.
WhoHasFileOpen.vbs, which Listing 1, page 15, shows, simplifies finding a file's user. You enter the filename (or any part of the filename) and the server name, and the script returns user and path information for files that match your input. The script outputs the results to a Microsoft Excel spreadsheet, so you can sort the information by user or path and format it any way you want.
The script's power resides in the LanmanServer service operation, which the code at callout A in Listing 1 implements. The script also contains code to create, open, and populate an Excel spreadsheet. The script's use of the Instr() function means that you don't have to worry about using the right case when entering the filename. And if you aren't sure of the exact filename, you can just key in a few letters of the name.
Now that I have this script, the next time someone needs to know who has a certain file open, I don't have to sift through 500 or more filenames. I can simply run my script, key in the filename and server name, and—presto!—I get an easy-to-read spreadsheet that I can copy, sort, or print with very little effort.