将命令结果保存在Windows批处理变量中 [英] Save the result of a command in variable, Windows batch

查看:487
本文介绍了将命令结果保存在Windows批处理变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个批处理脚本,该脚本将命令的结果保存在变量中.所以我以后可以使用.

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

例如,我正在尝试在脚本上运行此命令: sc queryex服务" |找到/i"pid"

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%

有什么想法吗?

推荐答案

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

这会将整行设置为PIDRS

这是仅获取pid的方法:

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=%%#
)

第二个不使用其他FIND,从理论上讲它应该使它更快.

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

这篇关于将命令结果保存在Windows批处理变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆