1

I would like to delete a file, which is locked by another process, FROM THE COMMAND LINE (Windows 7).

Note that I am aware that this might cause all kind of havoc, including losing all my data and making Alan Turing rotate in his grave. In my particular case, I know what I'm doing, or at least I'm willing to take the responsibility.

Since I have installed Cygwin, my attempt was to use rm -rf, but if the file is locked, this still doesn't work (Permission Denied).

I've googled for this problem and found suggestions to freeware tools (handler.exe and LockHunter), and I'm willing to try them. I just wonder if there are better ways to do it, in particular, whether some tools in Cygwin, or maybe in Windows itself, would allow me to do it.

Any suggestions?

user1934428
  • 19,864
  • 7
  • 42
  • 87

3 Answers3

1

You can install unlocker and use it from the command line. Unlocker is freeware. Regards Giova

Giova
  • 1,137
  • 1
  • 9
  • 17
  • Not possible, because our Anti Virus program blocks the download (Unlocker1.9.2.exe might have a virus or spyware). – user1934428 Jan 15 '15 at 17:22
1

handle suggested by @zmechanic could be wrapped in this script to simplify usage (specify partial file name instead of PID).

#!/bin/bash
if [ $# -eq 0 ]; then
    cat >&2 <<EOF
Arguments to narrow handles expected.

Usage: $(basename "$0") HANDLE_ARG...
HANDLE_ARGs are passed to the handle command by Sysinternals
(https://technet.microsoft.com/sysinternals/handle) which must be in the path.
Example $(basename "$0") c:\\dir\\xy
EOF
    handle -h || true
    exit 4
fi

o=$(handle "$@" | perl -nle 'print "$1\t$2\t$3" if m{\bpid:\s*(\d+)\s+type:\s+\S+\s+([[:xdigit:]]+):\s(.*)}') || exit
echo "$o"
echo "Closing $(wc -l <<< "$o") handles..."
while read -r p h; do
    handle -c "$h" -p "$p" -y
done <<< "$o"

Demo (if this script is saved as closefilehandle):

 > d=$(mktemp -d) && flock "$d" sleep 10& closefilehandle "$(cygpath -wa -- "$d")"
[1] 9472            
6408    E0  C:\tmp\tmp.9D3MlNQUEC
10224   E0  C:\tmp\tmp.9D3MlNQUEC
Closing 2 handles...

Handle v4.0
Copyright (C) 1997-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

   E0: File  (RWD)   C:\tmp\tmp.9D3MlNQUEC

Handle closed.

Handle v4.0
Copyright (C) 1997-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

   E0: File  (RWD)   C:\tmp\tmp.9D3MlNQUEC

Handle closed.

When globbing is needed, it should be done outside of the script:

> for f in */build; do closefilehandle "$(cygpath -wa -- "$f")"; done
EndlosSchleife
  • 515
  • 7
  • 19
0

You can use handle utility from command line before deleting the file. See https://stackoverflow.com/a/10672497/1786034

Community
  • 1
  • 1
zmechanic
  • 1,842
  • 21
  • 27
  • For deleting a file using "handle", I need to know the process which holds the file, and the hexadecimal file handle. I have only the path to the file. – user1934428 Jan 15 '15 at 16:30
  • Handle should do that. Check command line parameters or Google. – zmechanic Jan 15 '15 at 19:07
  • I did of course. The only parameter which closes the file is -c, and it expects as argument the file handle in hex. Of course I could first use handle without options to get the information about the file, parse the output to extract the filename, and then use handle -c to close the file. I just had hoped that there would be a simpler way. – user1934428 Jan 16 '15 at 07:34
  • Actually, there is another problem with handle.exe: It doesn't necessarily find the locked file. In our case, the locked file is always a HTML file, and the process which locks it, is java.exe. If I ask handle.exe for information on the file, it says "No matching handles found", while LockHunter finds it and can unlock it. – user1934428 Jan 16 '15 at 08:13