Karn
Karn

Reputation: 79

How to set env variable to the specific value out of multiple output values of some command?

I'm using following command to find out the pid for some string e.g. myname.

for /f "TOKENS=1" %a in ('wmic PROCESS where "commandline like '%myname%' and name ='java.exe'" get processid') do set myid=%a

This command returns me below o/p.

set myid=ProcessId set myid=1928 set myid= In the end it sets empty value to myid. How can i set the process id which is 1928?

Upvotes: 2

Views: 54

Answers (2)

npocmaka
npocmaka

Reputation: 57282

for /f "TOKENS=* delims=" %a in ('wmic PROCESS where "commandline '%myname%' and  name ='java.exe'" get processid /format:value')  do (set "%a" >nul)
set proc

try like this.

Upvotes: 1

MichaelS
MichaelS

Reputation: 6042

This works for me if I put it into a bat file:

@ECHO OFF
for /f "TOKENS=1 skip=1" %%a in ('wmic PROCESS where "commandline '%myname%' and  name ='java.exe'" get processid') do (
    SET PID=%%a
    GOTO BREAK
)
:BREAK
ECHO %PID%

The important data is stored in the 2nd output so just skip the first and exit the FOR-loop after the first iteration.

Upvotes: 0

Related Questions