Parse WMIC output to set returned value as a variable (windows batch)

北战南征 提交于 2021-02-13 17:36:38

问题


I have the following command:

c:\wmic baseboard get Manufacturer
Manufacturer
ASUSTeK Computer INC.

I'd like to save it's output into variable and process some way like I did it with bash here: esxi_hwinfo.sh @ GitHub e.g.: remove some sub-strings, shorten numbers, convert one string to another etc.

Is it possible to iterate over lines inside variable and get for instance the second one? Examples which I've seen work only with files (for /f "delims=" %%1 in ('type !foo!') do). If I use variable instead of file it says that there is no such file.

I don't want to use files as intermediaries since I think it's generally a bad idea.

There is no problem code which I want to work. There is a question about technical implementation (or possibility) of what I'm trying to achieve. It's more like "how to" than "what's wrong" question (like the most in here)


回答1:


Try this:

@echo off
set "manu="&for /f "skip=1 tokens=*" %%m in ('wmic baseboard get manufacturer') do if not defined manu set "manu=%%m"

rem now do things to %manu%
echo %manu%
pause


来源:https://stackoverflow.com/questions/47884082/parse-wmic-output-to-set-returned-value-as-a-variable-windows-batch

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