What is the Windows batch equivalent of "fuser -k <folder>"?

Matt picture Matt · Aug 5, 2013 · Viewed 9.2k times · Source

I have a Unix shell script which will kill any process that is accessing a folder:

fuser -k ..\logs\*

Is there a Windows batch script equivalent to this? I am aware of TASKKILL but I'm not sure if it will do what I need. Does anyone know if this is possible with Windows shell?

Answer

Jon picture Jon · Aug 5, 2013

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.