Recipe 8.20. Finding the Process That Has a File Open


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 telling you it is in use by another process.


Using downloadable software

One option is to use the Sysinternals Process Explorer:

  1. Open Process Explorer (procexp.exe).

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

  3. <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


    Processes running on your system are constantly opening and closing files (see Recipe 8.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 it, move it, 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 10.5). 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 8.16, which will replace a file after the next reboot.

    See Also

    Recipe 10.5 for more on how to kill a process; Recipe 8.16 for more on how to replace a file that is in use; Recipe 8.21 for more on viewing file activity; and MS KB 242131, "How to: Display a List of Processes That Have Files Open"

Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: