Thursday, April 24, 2014

Unlock files and folders using Process Explorer

Ever tried renaming\moving\deleting a file or folder and got this error?

The action can't be performed because the folder or a file in it is open in another program. Close the folder or file and try again.
Or, in case of a file:
The action can't be performed because the file is open in <Application Name>.
In my case, I've set up a shared folder for a virtual machine (VMware Workstation 10) and opened a file from it in a virtual machine. I have already closed the file, but it still was locked for renaming by the VMware.

So how do you deal with it?
Here is a stupid and careless method. Go download "Unlocker" software and use it. The software is long not supported and I wouldn't risk using it on modern OS. Besides, it requires installation. I am not going to provide any download links, because I don't encourage using it. It is like a hammer, while the solution I suggest is like a scalpel.

What I do suggest, is downloading a Process Explorer tool from SysInternals. Among other ton of features, it allows terminating a single handle in a process that locked the file, and searching for it.

Here is the download link: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

So what do you need to do with it? First - download, unpack, run and agree with EULA.

Then, press on the binocular icon (or use hotkey: CTRL+F) and type in the path to the folder or file that is locked and click the Search button. Any path substring will also do, but the more precise you are, the less results you'll get.

Highlight the handle that opened the file. The best and safest way, of course, would be to shut down the program that has this handle. But I don't want to shut down a virtual machine, that takes damn lot of time to turn it back on! Besides, a file can be locked by some system process that you can't safely shut down.

So what you can do, is highlight the handle, then close the search box, and in the main window the handle will still be highlighted. Right click on it and select Close Handle.





That should help. There also theoretically may be multiple handles locking a directory or a file - in this case you will need to repeat the procedure.

Hope it helps. Questions are welcome in the comments ;)

P.S. a colleague pointed me to a command line tool that can do the job. It is also made by SysInternals and is called "Handle". You can download it here: http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx