返回列表 发帖

[已解决]20元支付宝,根据父进程结束同名子进程但保留一个

需求有点复杂希望我能说得清楚:

假设有多个程序 aa1.exe aa2.exe  aa3.exe。。。。
aa1.exe启动后会打开3个同名子进程(xxx.exe ,
xxx.exe ,xxx.exe
aa2.exe也会打开3个同名子进程(
xxx.exe ,xxx.exe ,xxx.exe
。。。。
进程名aa是固定的 子进程名xxx是固定不变的)

那么如何只结束aa1.exe或
aa2.exe启动的3个同名子进程(xxx.exe)中的2个而保留1个(xxx.exe)?
(需求:任意结束aa2所开启的3个子进程中的2个保留1个 ,并且不会影响到aa1的子进程)
(不必局限于批处理,vbs或现成的工具都可以)

这里有个批处理可以保留1个子进程,但没办法区分父进程aa1.exe aa2.exe
-------------------(网上找的一个批处理,例子  看不懂  但测试有效)
@echo off&SETLOCAL enabledelayedexpansion
set a=0
tasklist>1.txt
for /f "tokens=1,2 delims= " %%i in (1.txt) do (
if "%%i" equ "xxx.exe" set /a a+=1 & if !a! neq 1 taskkill /f /pid %%j
)
-------------------
怎样才能以父进程为标准 结束两个xxx.exe保留一个呢??

本帖最后由 zaqmlp 于 2019-5-18 16:47 编辑
@echo off
set info=互助互利,支付宝扫码头像,感谢赞助
rem 有问题,可加QQ956535081及时沟通
title %info%
set "父进程=aa1.exe"
set "子进程=xxx.exe"
for /f %%a in ('wmic Process where "Name='%父进程%'" get ProcessId /value^|find "="') do set %%a
if not defined ProcessId (echo;ParentProcess is not found&goto end)
echo;父进程ID=%ProcessId%
for /f "skip=1" %%a in ('wmic Process where "Name='%子进程%' and ParentProcessId='%ProcessId%'" get ProcessId^|findstr "[0-9]"') do (
    echo;子进程ID=%%a
    taskkill /f /pid %%a /t
)
:end
echo;%info%
pauseCOPY
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

回复 2# zaqmlp


    你速度真快,用起来没问题,已发支付宝25,你看看收到没。

还有个小问题:不知道set "父进程=aa1.exe" 父进程的进程名有没有办法设定成不确定后缀的呢(aa*.exe)?有没有办法 一次性 关闭多个父进程的子进程?

TOP

@echo off
REM 选择进程名
set /p "strProcessName=输入父进程名:"
set "nProcessID="
REM 取得父进程PROCESSID
for /f "tokens=1,* delims=:" %%i in ('wmic process where name^="%strProcessName%" get processid ^| findstr /n .*') do (
if "%%i" equ "2" set "nProcessID=%%j"
)
if "%nProcessID%" equ "" (
echo,未找匹配的进程,请重试...
pause
goto :EOF
)
REM 取得子进程,并杀死除第一个子进程外的进程
for /f "tokens=1,* skip=1 delims=:" %%i in ('wmic process where parentprocessid^=%nProcessID% get processid ^| findstr /n .*') do (
REM echo,%%i:%%j
REM 第一个进程不管
if "%%i" NEQ "2" (
REM 最后的空行不管
echo,%%j | findstr [0-9] >nul 2>null && (
REM 其余进程中止
echo,--中止子进程:%%j
call wmic process where processid=%%j call terminate
)
) else (
echo,第一个子进程:%%j
)
)
pause
goto :EOFCOPY
echo,hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5x>in.com

TOP

返回列表