Skip to content

Commit bddd0b1

Browse files
committed
Made --list work, added readme.
1 parent 762ad5e commit bddd0b1

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
## Unlock
3+
4+
This utility for Windows uses `handle` from [Sysinternals](https://learn.microsoft.com/en-us/sysinternals/downloads/handle) to unlock a file. All open handles to the file by all processes are closed. This may cause the processes to malfunction, but sometimes freeing up the damn file is more important.
5+
6+
Usage:
7+
```
8+
java -jar unlock.jar [--list|-l] [path]
9+
```
10+
11+
### handle
12+
13+
`handle` is difficult to use directly because it lists the handles and expects you to assemble further commands to do the unlocking, which is tedious. Also `handle` uses substring matching, eg `abc` will match both `abc` and `abcd`. Checking for an exact match via shell scripts is cryptic and difficult to get right.
14+
15+
If substring matching is sufficient, `handle` can be used with Cygwin like this:
16+
```
17+
handle -nobanner -v "$1" | sed '1d' | cut -f2,4 -d, | tr ',' '\n' | xargs -r sh -c "handle -nobanner -p $1 -c $2 -y" sh
18+
```

src/com/esotericsoftware/unlock/Unlock.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ private Unlock (String path, boolean list) throws Exception {
4444
String[] columns = entry.split(",", 5);
4545
if (columns.length != 5) throw error("Error getting handles (invalid response): " + String.join(" ", command));
4646
if (!columns[4].trim().equals(path)) continue;
47-
command.add(columns[1]);
48-
command.add("-c");
49-
command.add(columns[3]);
50-
command.add("-y");
51-
run(command, null, true);
52-
command.remove(6);
53-
command.remove(5);
54-
command.remove(4);
55-
command.remove(3);
56-
System.out.println("Unlocked: " + columns[0]);
47+
if (list)
48+
System.out.println(columns[0]);
49+
else {
50+
command.add(columns[1]);
51+
command.add("-c");
52+
command.add(columns[3]);
53+
command.add("-y");
54+
run(command, null, true);
55+
command.remove(6);
56+
command.remove(5);
57+
command.remove(4);
58+
command.remove(3);
59+
System.out.println("Unlocked: " + columns[0]);
60+
}
5761
}
5862
}
5963

@@ -87,7 +91,7 @@ static public void main (String[] args) throws Exception {
8791
String path = null;
8892
boolean list = false;
8993
for (String arg : args) {
90-
if (arg.equals("--list"))
94+
if (arg.equals("-l") || arg.equals("--list"))
9195
list = true;
9296
else
9397
path = arg;

0 commit comments

Comments
 (0)