Link to home
Start Free TrialLog in
Avatar of boseani
boseani

asked on

How can I parse and assign a PID to a variable

Hi,

I want to create a dos batch that will allow me to run

C:\adplus.vbs -crash -pid <pid of process with highest mem usage>

I can run tasklist to get a list of the processes but I don't know how to parse the data and pass it into a variable.

C:\>tasklist /FI "IMAGENAME eq apache.exe"
Image Name                   PID Session Name     Session#    Mem Usage
========================= ====== ================ ======== ============
Apache.exe                  2708 Console                 0      3,924 K
Apache.exe                  1952 Console                 0      9,448 K

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of GuruGary
GuruGary
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boseani
boseani

ASKER

Not exactly but I'll accept your answer because if provided what I needed to come up with the following.

myBat.bat
@ECHO off

REM This batch file will run windbg and catch the breakpoint mod_cja!abort

REM List the processes named Apache.exe and save it to temp.txt
tasklist /FI "IMAGENAME eq Apache.exe" > temp.txt

REM If temp2.txt exists then delete it
IF EXIST temp2.txt DEL temp2.txt

REM traverse through the list of processes and select the MemUsage and PID for those named Apache.exe
FOR /F "eol=; tokens=1,2,3,4,5 delims= " %%i in (temp.txt) do IF Apache.exe==%%i @echo %%m %%j >> temp2.txt

REM sort the list of apache processes by Mem Usage
sort temp2.txt > sorted.txt

REM traverse the list and get the last PID
FOR /F "eol=; tokens=1,2 delims= " %%i in (sorted.txt) do SET varPID=%%j

REM clean up
IF EXIST temp.txt DEL temp.txt
IF EXIST temp2.txt DEL temp2.txt
IF EXIST sorted.txt DEL sorted.txt

REM call the windbg command
windbg -p %varPID% -c "bp mod_cja!abort;g"

Thanks,