How can I parse and assign a PID to a variable

Posted on 2006-05-30
Medium Priority
Last Modified: 2011-04-14

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.
Question by:boseani
LVL 10

Accepted Solution

GuruGary earned 2000 total points
ID: 16795464
If in a batch file, use the line:
for /f "tokens=2" %%a in ('tasklist /FI "IMAGENAME eq apache.exe" ^|findstr /i apache') do echo PID is %%a

Or from the command line use:
for /f "tokens=2" %a in ('tasklist /FI "IMAGENAME eq apache.exe" ^|findstr /i apache') do echo PID is %a

%%a (or %a) is the variable containing the PID.  If this is not what you are looking for, please post more details.

Author Comment

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

@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"


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question