How can I parse and assign a PID to a variable

Posted on 2006-05-30
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

    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

    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"


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
    #Citrix #POC #XenDesktop #vCenter #VMware #ESX
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now