Windows Server Cookbook
Authors: Allen R.
Published year: 2006
Pages: 82-83/380
Buy this book on amazon.com >>

Recipe 4.19. Finding Open Files

Problem

You want to find the open files on a server.

Solution

There are two different categories of open files on a system. Since the days of Windows NT, the operating system has supported the capability to view the files that are open from shared folders. This is useful when you want to see who is accessing files on a file server, especially if you need to take the system down for maintenance and you want to notify the impacted users.

First seen in Windows XP and supported in Windows Server 2003 is the ability to view all open files on a system (not just shared folders). To use this feature, you first have to enable support for it. The reason this isn't enabled by default is because there is a slight system-wide performance impact when tracking all open files.

Using a graphical user interface

None of the standard graphical tools provide a list of the open files on a system. The closest thing to it would be the Sysinternals File Monitor tool. For more information, see Recipe 4.21.

To view the open files from shared folders, do the following:

  1. From the Administrative Tools, open the Computer Management snap-in.

  2. In the left pane, expand System Tools Shared Folders Open Files .

  3. To close an open file, right-click on it in the right pane and select Close Open File .

Using a command-line interface

To view the open files from shared folders, run this command:

> net file

The output from that command displays open files and their associated ID. Using this ID, you can close a specific file:

> net file


<ID>


/close

To view all open files, first enable support for it:

> openfiles /local on

You'll need to reboot the system before this setting takes effect. At that point, you can see open files using this command:

> openfiles

Use the /s <ServerName> option to target a remote server. Similar to the net file command, you can close any open file by running this command:

> openfiles /disconnect /id


<ID>



You can also disconnect all the files open by a particular user:

> openfiles /disconnect /a


<UserName>



See Also

Recipe 4.21


Recipe 4.20. Finding the Process That Has a File Open

Problem

You want to find the process or processes that have a file open. This is often necessary if you want to delete or modify a file, but are getting errors because it is in use by another process.

Solution

Using a graphical user interface

  1. Open the Sysinternals Process Explorer ( procexp.exe ) tool.

  2. Click the Find icon (binoculars) or select Search Find from the menu.

  3. Beside Handle substring , enter the name of the file and click Search .

Using a command-line interface

Use the Sysinternals handle.exe command to view the processes that have a lock on a file:

> handle


<FileName>



This example commands shows all the processes that have a handle to the personal.pst file:

> handle personal.pst

Using VBScript
' This code prints the output from the handle.exe command
' ------ SCRIPT CONFIGURATION ------
strFilePattern = "


<FileName>


" ' e.g., personal.pst
strHandleExec = "handle.exe"  ' If handle.exe isn't in your PATH, you will
                              ' need to specify the full path.
' ------ END CONFIGURATION ---------
set objWshShell = CreateObject("WScript.Shell")
set objExec = objWshShell.Exec(strHandleExec & " " & strFilePattern)
do while not objExec.StdOut.AtEndOfStream
    WScript.Echo objExec.StdOut.ReadLine( )
loop

Discussion

Processes running on your system are constantly opening and closing files (see Recipe 4.21 for more on how to see this activity). When a process accesses a file, the process is said to have a handle to the file. Processes can also have handles to other system resources, such as Registry keys and values. For certain types of file accesses, a process may obtain an exclusive lock on the file (such as when it needs to write to the file), which means no other processes can modify the file; you may still be able to read the file, but you won't be able to overwrite, move, or delete it.

This may be a bit annoying if there is a file you need to do something with. You have a couple of options. First, if you determine the process that has a handle to the file is not important, you could try to kill it (see Recipe 6.3). This will often remove the lock on the file, but this isn't the most graceful approach. If you just want to replace the file, another option entails following the instructions in Recipe 4.16, which will replace a file after the next reboot.

See Also

Recipe 4.16, Recipe 4.21, Recipe 6.2, and MS KB 242131 (How to: Display a List of Processes That Have Files Open)

Windows Server Cookbook
Authors: Allen R.
Published year: 2006
Pages: 82-83/380
Buy this book on amazon.com >>

Similar books on Amazon