0

I want to replace some files in folder, and I see that there are multiple process under the folder that prevent me from doing that.

How can I kill all process process that run under a folder?

Idan
  • 509
  • 2
  • 10
  • 24
  • Use handle.exe from Sysinternal and wrap it like this: https://stackoverflow.com/questions/958123/powershell-script-to-check-an-application-thats-locking-a-file – Axel Andersen Mar 19 '20 at 14:46
  • or if you are prefer `cmd`, use `wmic process` – ScriptKidd Mar 19 '20 at 14:46
  • The supported Windows way is to rename the open files (open executable files can be renamed), copy your new files in, and either restart the app or restart the computer. –  Mar 19 '20 at 19:10

2 Answers2

2

Using powershell:

  1. Navigate the current powershell directory to the destination path by:

cd "%*DestinationPath*%"

  1. Get-Process | ?{$_.path -and (test-path (split-path $_.path -leaf ))} | Stop-Process -Force
Idan
  • 509
  • 2
  • 10
  • 24
1

If you prefer using (because is one of your tags), use wmic process:

@echo off
set "dir=YOUR PATH HERE"

for /f "skip=1 tokens=*" %%a in ('wmic process get executablepath') do (
    for /f "eol= tokens=*" %%A in ("%%a") do (
        echo(%%~dpA | findstr /I %dir% >nul 2>&1
        if %ERRORLEVEL% equ 0 taskkill /F /IM "%%~nxA"
    )
)

Note: This is NOT foolproof, all processes with the same name will get killed

ScriptKidd
  • 803
  • 1
  • 5
  • 19