What is the Windows batch equivalent of "fuser -k <folder>"?
Not with built-in tools (there is nothing that can enumerate handles), but you can use the Sysinternals Handle tool to get a list of the PIDs that have open handles on a file. You can then parse the output with FOR /F
and use TASKKILL
to end the processes.
Example:
for /f "skip=4 tokens=3" %i in ('handle ..\logs\') do taskkill /pid %i
skip=4
skips the copyright information in Handle's output and tokens=3
picks out the PID from each line of output. The rest are self-explanatory.
I found a fuser
for Windows. Check this link to Sourceforge:
Many would ask, "why not use
handle.exe
from SysInternals?"While being a truly excellent utility (as all of SysInternals stuff is), it falls short -- at least for me -- for a couple of reasons:
It does filename pattern matching. I need it to do exact file matches with partial input. For example, when I'm in the
C:\TMP
directory, and I'm checking on a file namedout.dat
, I want it to see if the fileC:\TMP\OUT.DAT
is being used without having to type full path in. I think even if you type in the full path for handle.exe, it will still do pattern matching.
handle.exe
seems to always exit 0. For scripting purposes, I need it to exit 1 if the file is not being used, but 0 if it is. This is very useful in scripts.