Unlocking files the easy way

Often the clean task in an Ant build will fail due to an error like below.

BUILD FAILED
D:\my\project\build.xml:58: Unable to delete file D:\my\project\build\someFile.txt

It can mean that you don't have write permissions on the file or parent folder. However, more often I find it is because some process has locked the file: perhaps an indexing process like Google Desktop Search or Windows Search, or maybe a Virus Scanner. If you know what process it is, you can kill it through Task Manager, but that's fiddly, especially if it keeps happening. Worse, I usually want the culprit process to continue doing it's job i.e. I don't want to kill it.

My solution begins with using Unlocker (Windows XP) to tell me what processes have locked a file: right click on any file, select Unlocker and see a list of the offending processes. You can choose to kill any of the processes or request that they unlock the file you need access to.

This is great, but awkward if I don't immediately have Explorer open to the directory involved. If this has come about because of a failed Ant build, I will have the path in my console, but then I have to copy the path from the console and paste it into Explorer (after fixing any line wrapping in a text editor). This would be much easier if I had the Ant output in a text file already. By default, I always run Ant tasks by re-directing their output to a temporary file which gets opened in my text editor. For example (in Cygwin):

ant -p > temp.txt ; notepad temp.txt &

I have a script that does this (of course). It means I will always have the offending path in a text document. Next step: the unbelievably amazing AutoHotkey, a Windows scripting engine that lets you assign keyboard shortcuts to scripted functionality. This script invokes Unlocker - on whatever path I currently have selected when I press ctrl+win+u.

; - Unlock X by Copying Text (control+windows key + u)
^#u::
   Send, {CTRLDOWN}c{CTRLUP}
   Run C:\Program Files\Unlocker\unlocker.exe %clipboard%
return

This is just one AutoHokey function I have defined in a single "utilities" AutoHotkey script that gets loaded through my Windows Startup directory so that it is always available. For others, check out this SuperUser post: Most useful AutoHotkey scripts?

To summarise: Ant outputs to a text file, Unlocker can unlock files by absolute path, Autohotkey copies currently selected text (should be a file path) and invokes Unlocker, sending the path as an argument.

Popular Posts