对于带Wmic的/F有害输出 [英] For /F with wmic unwanted output

查看:85
本文介绍了对于带Wmic的/F有害输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇文章在这里,如果我做的不太对不起,

First post here, so apologies if I don't do this quite right.

我正在尝试在远程Windows PC上输出OS版本,但是我一直在获取不需要的数据.执行此批处理文件:

I'm trying to output the OS version on a remote Windows PC, but I keep getting unwanted data. Executing this batch file:

@echo off
set /p hostname=PC hostname?
echo.
FOR /F "skip=1 tokens=*" %%A in ('wmic /node:%hostname% OS get caption') DO echo %%A
echo.
pause

返回此结果:

Microsoft Windows 7 Professional
Echo is off.

我能够使用skip=1删除Caption输出,但是我不知道echo命令的来源.拆分命令没有帮助,但是打开echo显然表明,循环的wmic命令在输出OS版本后正在输出echo,因此,显然,当echo关闭时,我得到了最终输出:.谁能帮助我了解为什么echo输出正在发生?

I was able to remove the Caption output using skip=1 but I don't understand where the echo command is coming from. Splitting out the commands didn't help, but turning echo on shows that, apparently, the looped wmic command is outputting echo after outputting the OS version, so, obviously, when echo is off, I get the final output: Echo is off. Can anyone help me understand why the echo output is happening?

推荐答案

Magoo确认了问题的根源-WMIC Unicode输出到ANSI的转换存在缺陷,因为每行以 CR CR LF ,而不是普通的 CR LF .

Magoo identified the source of the problem - the conversion of WMIC unicode output to ANSI is flawed in that each line ends with CRCRLF instead of the normal CRLF.

FOR/F在每个 LF 处断裂(并剥离),留下 CR CR .当且仅当它恰好是一个 CR 时,FOR/F才从每行中删除最后一个字符,以便在每一行的末尾保留一个 CR . FOR/F忽略空行,但是 CR 不是空行,因此不需要的Echo is off.输出.

FOR /F breaks at (and strips) each LF, leaving CRCR. FOR /F strips the last character from each line if and only if it happens to be a CR, so that leaves one CR at the end of every line. FOR /F ignores empty lines, but CR is not an empty line, hence the unwanted Echo is off. output.

Magoo和Squashman为您的问题提供了可行的解决方案,但它们不是适用于所有FOR/F-WMIC情况的通用解决方案.将FOR/F输出存储在变量中时,会出现一个常见问题-不必要的 CR 通常在值的末尾,这可能会在以后引起问题.

Magoo and Squashman have provided workable solutions to your problem, but they are not generic solutions that apply to all FOR /F - WMIC situations. A common issue arises when FOR /F output is stored in variables - the unwanted CR is often at the end of the value, which can cause problems later on.

我们可以使用FOR/F的已知机制,只需添加一个额外的FOR/F,即可从每行中删除不需要的 CR .

We can use the known mechanics of FOR /F to strip the unwanted CR from every line by simply adding an extra FOR /F.

因此始终有效的通用模板如下

So the generic template that always works looks like

for delims^=^ eol^= %%. in ('wmic ....') do for /f "whatever options you need" %%A in ("%%.") do ...

或者如果您需要跳过N行,那么

or if you need to skip N lines, then

for skip^=N^ delims^=^ eol^= %%. in ('wmic ....') do for /f "whatever options you need" %%A in ("%%.") do ...

第一个FOR/F中的奥术语法将DELIMS和EOL都设置为空.如果您知道所有输出都不以;开头,则可以简单地使用"delims=""skip=N delims=".在您的情况下就是这种情况.

The arcane syntax in the first FOR /F sets both DELIMS and EOL to nothing. If you know that none of your output begins with ;, then you can simply use "delims=", or "skip=N delims=". This is the case in your situation.

因此解决方案变为:

for /f "skip=1 delims=" %%. in ('wmic /node:%hostname% OS get caption') do for /f "delims=" %%A in ("%%.") do echo %%A

这篇关于对于带Wmic的/F有害输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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