Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 902
Matt Avatar asked Aug 05 '13 13:08

Matt


1 Answers

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.

like image 50
Jon Avatar answered Sep 23 '22 19:09

Jon