Tuesday, September 13, 2005

program to find which process is using a file windows

Today I was trying to delete a file on my hard drive and it came back and told me that, although it bitterly regretted it, the operating system would not be able to comply with my request because another process had the file opened and was restricting access to it. Without the proper access, the operating system cannot delete a file.

I tried my first trick, which works a good percentage of the time: I renamed the file. Or, I tried to. Instead, I got a similar message indicating even this approach of renaming the file was not possible. Often it works because the owner of the handle that has the file opened can continue to access the file so long as you simply rename it and do not attempt to delete it. I think, though I am not sure, that this method works so long as the owner of the handle does not have that handle opened for write access.

At that point, the question that is the title of this blog entry came into play: "Is there a program out there that will tell me which program or programs has a specific file opened?" If I know which process has my file held hostage I can use task manager to terminate that process and thus free up the file's permissions so that I can safely delete the file.

I did a few google searches but didn't turn up much. Disheartened, I started scanning with my good friend 'procexp.exe' - or Process Explorer - to see which one it might be. Procexp can show you which handles a given process has open, including files. As I did this manual hunting, I noticed the Find menu, which turns out to be the perfect tool for the task. Using its Find Handles... dialog, you can type in the substring to search for - which can be a portion of the filename, or the type of resource in general. For example, type 'ntuser' and you'll find all the handles opened on your system that touch anything with ntuser in it, such as the ntuser.dat log file, which every user has in Windows XP.

This feature was so usefull and cool, I just had to share. If anyone is even reading this out there, and you have a good tip of your own, please do share!

Update:

There is now ( perhaps always was) a command line utility also available at sysinternals.com called Handle that provides a very rapid way to find open files and other handles on your system. I recommend it above procexp.exe unless you're already running procexp.exe. It is also nice, if you are like me and use a suite of unix commands for dos. Then, you can pipe the results from the handle command to whatever other command-line programs you like. For example: 'handle | grep -i insight" will show me not only what files are opened that have the word insight in their title but also all handles opened by my editor, Source Insight, because its executable name is insight.exe.

-Michael

3 comments:

addison98sherry said...
This comment has been removed by a blog administrator.
ADelano said...

Fantastic !!!!

Great Advice !!!!

Somehow, XP let me "half-kill" a process thru fast double clicking ( i.e. it was removed as an application, but the process remained.)

I could not delete the locked file. ( at 5am no less) And turned to google. Your blog was among the first that came up.

It worked !

Thanks again.

Reeves said...

Thank you!!!