How to Kill Running Process from a specific folder via Batch File

10,842

Solution 1

The way with you'll get the most comprehensive information about a process with batch file will be the WMIC command. Though it does not contain a working directory so probably you'll have to orient yourself by the command line. Check the comments in the script bellow.First replace the workdir variable with your desired path and if the process looks ok uncomment the last line.You can precise:

@echo off

:: Put the work dir here
set "workdir=C:\Windows\system32"
set "workdir=%workdir:\=\\%"

setlocal enableDelayedExpansion
for /f "usebackq tokens=* delims=" %%a in (`
    wmic process  where 'CommandLine like "%%!workdir!%%" and not CommandLine like "%%RuntimeBroker%%"'   get CommandLine^,ProcessId  /format:value
`) do (
    for /f "tokens=* delims=" %%# in ("%%a")  do (
        if "%%#" neq "" (
            echo %%#
            set "%%#"
        )
    )
)

rem if the echoed process looks ok unocment line bellow

rem taskkill /pid %ProcessID% /f

the %% is the wildcard in the wmic query so you can precise the selection more. Pay attention that the second part is not like (and is just for example). Here's more about WQL

Solution 2

Below you will find improved version of script created by npocmaka in post above. You can also download this script from closeapps.cmd

@echo off

if "%1"=="" goto help

:: Put the work dir here
set "workdir=%1
set "workdir=%workdir:\=\\%"

setlocal enableDelayedExpansion
for /f "usebackq tokens=* delims=" %%a in (`
    wmic process  where 'CommandLine like "%%!workdir!%%" and not CommandLine like "%%RuntimeBroker%%"' get CommandLine^,ProcessId  /format:value
`) do (
    for /f "tokens=* delims=" %%# in ("%%a")  do (
        if "%%#" neq "" (                   
            set "%%#"                   
            SET var="%%#"
            SET searchVal=ProcessId
            SET var|FINDSTR /b "var="|FINDSTR /i %searchVal% >nul           
            IF ERRORLEVEL 1 (echo.) ELSE (taskkill /pid !ProcessId! /f)         
        )        
    )

)

goto end

:help
echo ********************
echo * Example of Usage *
echo ********************
echo.
echo =============================================
echo closeapp \\server\path\test
echo.  

:end

Solution 3

I think PowerShell is a much easier way to do this task.

You would start with the cmdlet get-process

get-process | select ProcessName, Path 

This would display the name and the path of where the process is running from.

Then you would make sure none of the processes are running from the path you care about. If they are running you would use stop-process -id $id -force

Share:
10,842
Bill
Author by

Bill

Updated on June 05, 2022

Comments

  • Bill
    Bill almost 2 years

    I am writing an uninstaller batch file and it occasionally fails to delete folders because of running process. Is there a way to kill all running process that reside in a particular folder. Keep in mind that there could be multiple process running from subfolders within the main. So I think it needs to search the running processes and forcefully and silently kill all that reside within "C:\Program Files\PTC" or a subfolder within.

    I do know how to kill off a specific process using the "Taskkill" command, but searching for all process running within a folder and killing them all off is a little over my head.

    Thanks for the help in advance.

    Bill

  • JosefZ
    JosefZ about 8 years
    I'd use ExecutablePath like "%%!workdir!%%" instead of CommandLine like "%%!workdir!%%" as CommandLine could be partially qualified… And move rem taskkill … inside for … %%# loop
  • Milan
    Milan almost 6 years
    I am completely unable to make this script works. It returns an invalid query error.