Jorococo
Jorococo

Reputation: 23

Save the result of a command in variable, Windows batch

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?

Upvotes: 2

Views: 2150

Answers (1)

npocmaka
npocmaka

Reputation: 57262

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.

Upvotes: 2

Related Questions