How to unlock files using handle.exe and process name?
I tried Unlocker 1.9.1 but it doesn't work correctly for me on Windows7 (worked ok on Windows XP) and also I tried LockHunter 2.0.2.103 x64 and reported a bug but .... LockHunter actually unlocks the file from GUI but not from command line.
So I want to use handle.exe by SysInternals to unlock one file "TestPro.log". I know the absolut path if it helps. I can list and all processes that locked the file by executing
C:\Windows\system32>c:\edutester\progs\handle testpro.log
java.exe pid: 2120 type: File 338: C:\Users\Public\TestPro
\TestPro Automation Framework\Logs\TestPro.log
java.exe pid: 1004 type: File 934: C:\Users\Public\TestPro
\TestPro Automation Framework\Logs\TestPro.log
What I need to know how to unlock the file using above info from command line automatically. No user intervention is possible.
- Windows 7 64bit
- Microsoft Windows [Version 6.1.7601]
Solution 1:
You can use handle.exe
once to list the PIDs and handles, then again multiple times to close each one.
Use the for /f
command to loop through the result of the first command. I don't have a Windows machine handy to test on, but it should look something like this:
From a batch file:
for /F "tokens=3,6 delims=: " %%I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %%J -y -p %%I
From the command-line:
for /F "tokens=3,6 delims=: " %I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %J -y -p %I
Solution 2:
https://technet.microsoft.com/en-us/sysinternals/handle.aspx
TEST AND DISPLAY:
for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do @echo %i %j
EXECUTE:
for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i
EXECUTE AND TASKKILL:
for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i & taskkill /t /f /PID %i