Save the result of a command in variable, Windows batch

帅比萌擦擦* 提交于 2020-01-13 19:19:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33215819/save-the-result-of-a-command-in-variable-windows-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!