对于带有 wmic 不需要的输出的/F [英] For /F with wmic unwanted output

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

问题描述

第一次在这里发帖,如果我做得不对,请见谅.

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

我正在尝试在远程 Windows PC 上输出操作系统版本,但我不断收到不需要的数据.执行这个批处理文件:

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 命令在输出操作系统版本后输出 echo,因此,显然,当 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 的转换存在缺陷,因为每一行都以 CRCRLF 而不是普通的 CRLF.

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 处中断(并剥离),留下 CRCR.FOR/F 当且仅当它恰好是 CR 时从每一行中删除最后一个字符,因此在每行的末尾留下一个 CR.FOR/F 忽略空行,但 CR 不是空行,因此不需要的 Echo 关闭. 输出.

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天全站免登陆