将变量设置为命令值 [英] Set Variable to value of command
问题描述
我需要将WMIC COMPUTERSYSTEM GET MODEL/Value的值设置为变量.当我运行此命令时,我得到以下结果.为什么我会得到多余的空白行?
I need to set the value of WMIC COMPUTERSYSTEM GET MODEL /Value to a variable. when I run this command I get the results below. Why do I get the extra blank lines?
U:\>for /f "delims=" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do set output=%i
:\>set output=
:\>set output=
:\>set output=Model=HP ProDesk 600 G1 SFF
:\>set output=
:\>set output=
:\>set output=
推荐答案
这是WMIC输出的问题,其中包含carriage return
个字符.
因此,空行不会被删除,因为它们并不完全是空的.
It's a problem of the WMIC output, that contains carriage return
characters.
Therefore the empty lines aren't removed, as they are not completely empty.
回车符还负责:>
而不是U:>
的奇怪输出,因为回车符首先将光标移动到POS1
,然后空格删除U
.
The carriage returns are also responsible for the strange output of :>
instead of U:>
, as the carriage return first moves the cursor to POS1
and then a space deletes the U
.
使用额外的FOR/F,您可以删除回车符
With an extra FOR/F you can strip the carriage return characters
for /f "delims=" %i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do @(
for /F "delims=" %X in ("%i") do set output=%X
)
顺便说一句.通常,您希望在批处理文件中而不是在命令行中使用这样的表达式.
然后,您必须将其转换为
Btw. Normally you want such an expression in a batch file, not at the command line.
Then you have to convert it to
@echo off
for /f "delims=" %%i in ('WMIC COMPUTERSYSTEM GET MODEL /Value') do (
for /F "delims=" %%X in ("%%i") do set output=%%X
)
这篇关于将变量设置为命令值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!