3

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

Bill
  • 147
  • 1
  • 1
  • 10
  • To Kill Process ==> Look at this [How to stop process started with an Eclipse external tools configuration](http://stackoverflow.com/questions/35803971/how-to-stop-process-started-with-an-eclipse-external-tools-configuration?answertab=active#tab-top) – Hackoo Mar 21 '16 at 22:44
  • Thanks for the link, My problem is that there could be multiple process running that are located in "C:\Program Files\PTC". I need to be able to query the running processes and kill "all" that are running from within "C:\Program Files\PTC" before I delete the "PTC" folder. Or if possible, another option would be possible is query the running process for a description that contains *ptc* and then kill those before deleting the "ptc" folder – Bill Mar 21 '16 at 23:15

5 Answers5

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

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    I'd use `ExecutablePath like "%%!workdir!%%"` instead of `CommandLine like "%%!workdir!%%"` as `CommandLine` could be partially qualified… And move `rem taskkill …` inside `for … %%#` loop – JosefZ Mar 22 '16 at 00:20
1

Batch isn't suited to this as you can see by the gobblygook in previous answer. The line to terminate is commented out ' character.

This is VBS.

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
'    If Instr(objItem.ExecutablePath, "C:\Program Files") > 0 Then objItem.terminate
    If Instr(objItem.ExecutablePath, "C:\Program Files") > 0 Then msgbox objItem.name
Next

You can use WMIC to get help as they use the same objects.

wmic /?
wmic process /?
wmic process call /?
wmic process get /?
  • Thanks for the code. I ended up putting this in a vbs script and calling the vbs from my bat. I tried to nest this in my bat file via multiple methods, but it would not execute and close down my command window. – Bill Mar 23 '16 at 03:32
1

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

runcmd
  • 572
  • 2
  • 7
  • 22
1

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
rafalkasa
  • 1,743
  • 20
  • 20
0

This works for me...

See example below

FOR /F "delims=" %%G in ('FORFILES /P "%ProgramFiles%\Microsoft VS Code" /M *.EXE /S') DO ( TASKKILL /F /IM %%G /T )

Essentially this kill all processes within the root and subfolders of %ProgramFiles%\Microsoft VS Code

EricSP
  • 15
  • 5