2

I am trying to write a batch script that saves the result of a command in a variable. so I can use it later.

For example I am tryin to run this on the script: sc queryex "Service" |find /i "pid"

but I want to save this result in a variable.

set PIDRS=sc queryex "Themes" |find /i "pid" ECHO "%PIDRS% 

Any Ideas?

2

1 Answer 1

2
for /f "tokens=* delims=" %%# in ('sc queryex "Themes" ^|find /i "pid"') do set "PIDRS=%%#" echo %PIDRS% 

This will set the entire line to PIDRS

here's how to get only the pid:

@echo off set "rspid=" for /f "skip=9 tokens=2 delims=:" %%# in ('sc queryex "Themes"') do ( if not defined rspid set /a rspid=%%# ) 

the second does not use additional FIND which in theory should make it faster.

Sign up to request clarification or add additional context in comments.

2 Comments

skip=9 fails for a stopped service, leads to Missing operand error. I'd use (from command line) for /F "tokens=1* delims=: " %# in ('sc queryex "Themes"') do @if /I "%#"=="PID" set /A "rspid=%$"
@eranotzap it will be assigned to variable rspid.You can get its value with %rspid%

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.