Saturday, January 12, 2008

How to Find Which Program is Accessing a File

Occasionally, I will attempt to delete a file or folder, and I get an error stating:
Error Deleting File or Folder
Cannot remove folder xxxxxx: It is being used by another person or program.
Close any programs that might be using the file and try again.
Now, the easy solution is to just close down all of your open programs in the hope that one of them is accessing that file or folder. That is, it's easy in that it doesn't require much thinking to do. But I hate shutting down all of my open programs because one file is being obstinate. Even after going through the trouble of killing all open programs, there could still be a system process accessing that file. This file is connected to a process as a handle. You don't want to kill that process--just close the handle. And once all handles connected to your undeletable file are closed, then the file can be trashed.

There are two programs you can use for this:
  • Unlocker - This program is primarily a shell extension, which means that it shows up in your right-click menu. Just right-click the file you want to delete, choose "Unlocker" from the context menu, and a dialog will pop up listing all the handles connected to that file. Click the Unlock All button and that file is ready for death. If you want a simple and elegant program that does just this one thing, install Unlocker (the download link is right below the PayPal Donate button).
  • Process Explorer - There are so many things this program is useful for, but the goal here is to kill a few handles. Open up Process Explorer and click the Find menu and select Find Handle or DLL... (or just press CTRL + F as a shortcut) . Type the name of the file or folder in the search field, hit Search and you'll get a list of all file handles that connect to that file. If you don't supply an entire path, it will try to find the handle based on a string search. Click on a handle and in the main Process Explorer window, bottom pane, that handle will be selected. Now you can either right-click the handle selected in the main window and choose Close Handle or you can go to the Handle menu and choose Close Handle. It's slight less efficient than Unlocker at this task, but it can give you just about any bit of information you could possibly want to know about any running process. (The download link is at the top of the far-right column.)
Use either of these programs and you won't have to interrupt your work flow to delete the undeletable.