我正在尝试使用FOR命令来获取多次运行的程序的PROCESS ID。当我使用以下命令时,我可以看到3个PID
C:\>wmic PROCESS where "Name='notepad.exe'" get ProcessID | findstr [0-9]
输出: C:>
396
13928
20424
但是当我在FOR命令中的相同命令中使用它时,我只获得最后一个PID 20424的回声输出。我试图搜索notepad.exe(或任何其他程序)的PID打开,看看它是否在procdump命令中使用,如果不是我想用新的PID执行procdump。
脚本
@echo on
for /f "TOKENS=1" %%x in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID ^| findstr [0-9] ^|find /v /c "notepad.exe"') do set myPID=%%x
echo %myPID%
:loop1
if /I %myPID% geq 0 (
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID ^| findstr [0-9]') do set nPID=%%a
echo %nPID%
echo nPID %npid%
for /f "tokens=8" %%b in ('wmic PROCESS where "Name='procdump.exe'" get CommandLine ^| findstr %npid%') do (
set dpid=%%b
echo dPID %dpid%
if %npid% neq %dpid% (
start /B C:\Users\RV017019\Downloads\Procdump\procdump -c 1 -s 5 -n 5 %npid%
)
)
)
goto :loop1
)
输出:
C:\>for /F "TOKENS=1" %x in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID | findstr [0-9] |find /v /c "note
pad.exe"') do set myPID=%x
C:\>set myPID=3
C:\>echo 3
3
C:\>if /I 3 GEQ 0 (
for /F "TOKENS=1" %a in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID | findstr [0-9]') do set nPID=%a
echo 20424
*你可以看到它正在拿起最后一个PID
if 20424 EQU 396 (echo "you are right" 20424 ) else (echo "Not right" )
goto :loop1
)
C:\>set nPID=396
C:\>set nPID=13928
C:\>set nPID=20424
20424
"Not right"
C:\>if /I 3 GEQ 0 (
for /F "TOKENS=1" %a in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID | findstr [0-9]') do set nPID=%a
echo 20424
if 20424 EQU 396 (echo "you are right" 20424 ) else (echo "Not right" )
goto :loop1
)
C:\>set nPID=396
C:\>set nPID=13928
C:\>set nPID=20424
20424
"Not right"
我想要每个PID并搜索procdump命令,如果它不存在我执行新的procdump命令。
答案 0 :(得分:0)
尝试将回声包含在for
语句中(您必须使用延迟扩展):
setlocal enabledelayedexpansion
for /f "TOKENS=1" %%x in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID ^| findstr [0-9] ^|find /v /c "notepad.exe"') do (
set myPID=%%x
echo !myPID!
)
答案 1 :(得分:0)
你可以这样做:
@echo off
for /f "delims=" %%a in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID ^|findstr [0-9]') do (
call:test %%a)
exit/b
:test
echo Testing PID : %1
if %1==396 echo Do something with %1
if %1==13928 echo Do something with %1
if %1==20424 echo Do something with %1
答案 2 :(得分:0)
以下是获取每个PID的方法:
@echo off
for /f "tokens=2 delims=<>" %%x in ('wmic PROCESS where "Name='notepad.exe'" get ProcessID /format:htable ^| find "<div" ') do call :label "%%x"
pause
goto :EOF
:label
echo %~1
答案 3 :(得分:0)
经过几次尝试,我能够拉出这个能够做我想做的剧本。如果有人能帮助改进,那就太棒了.-谢谢
@echo off
for /f "TOKENS=1" %%x in ('wmic PROCESS where "Name='firstnet.exe'" get ProcessID ^| findstr [0-9] ^|find /v /c "firstnet.exe"') do set myPID=%%x
echo %myPID%
:loop1
if %myPID% geq 0 (
FOR /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='firstnet.exe'" get ProcessID ^| findstr [0-9]') do (
set /A npid=%%a
call:test %%a
)
goto :loop1
)
exit /b
:test
for /f "tokens=8" %%b in ('wmic PROCESS where "Name='procdump.exe'" get CommandLine ^| findstr %npid%') do set ppid=%%b
if [%1] EQU [] ( set ppid=00 ECHO Value Missing
)
if /I %npid% NEQ %ppid% (
start procdump -c 10 -n 100 -s 3 %npid%
) ELSE (
echo THIS PID %npid% PROCDUMP RUNNING
)
timeout /t 5