为什么错误消息“缺少操作数”?在处理WMIC输出并为变量分配值时输出? [英] Why is the error message "Missing operand" output on processing WMIC output and assigning a value to a variable?

查看:183
本文介绍了为什么错误消息“缺少操作数”?在处理WMIC输出并为变量分配值时输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个批处理文件,该文件应显示当前估计的剩余费用。但是当我运行命令时,我得到了期望的结果以及错误消息缺少操作数。输出。

I am working on a batch file that is supposed to show the currently estimated charge remaining. But when I run the command I get the desired result plus the error message Missing operand. output.

我的代码:

FOR /F "delims= skip=1" %%i IN ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining') DO (SET /a CHR=%%i)
ECHO "Battery Level: %CHR%"

如何摆脱不必要的输出缺少操作数。

How can I get rid of that undesired output Missing operand.?

我已经尝试删除来自最后一部分,即 DO(SET / a CHR = i),但是当我这样做时,我最终得到电池电量:0 好像它正在跳过某些结果数字。

I have already tried to remove % from the last part, i.e. DO (SET /a CHR=i), but when I do so I end up with "Battery Level: 0" as if it were skipping some of the numbers of the result.

推荐答案

Windows管理规范命令行实用程序输出文本 UTF-16 使用字节顺序标记(BOM),这意味着(几乎)每个字符始终有两个字节,而未显示 FF FE (BOM为十六进制字节)位于输出文本的开头。

The Windows Management Instrumentation Command-line utility outputs text UTF-16 Little Endian encoded with byte order mark (BOM) which means (nearly) always with two bytes per character with not displayed FF FE (BOM as hexadecimal bytes) at beginning of the output text.

FOR 设计用于使用ASCII / ANSI / OEM编码是字符编码,每个字符仅使用一个字节。通过在命令提示符窗口中运行命令 chcp 输出的已使用代码页,确定哪个字符由一个字节表示,其值的十进制范围为 0 255 或十六进制 00 FF 。默认情况下,Windows命令行环境中使用的代码页取决于为使用的用户帐户设置的区域/国家/地区。

FOR is designed for parsing text using ASCII/ANSI/OEM encoding which are character encodings using just one byte per character. The used code page output by running in a command prompt window the command chcp determines which character is represented by a byte with value in decimal range 0 to 255 or hexadecimal 00 to FF. The code page used in Windows command line environment by default depends on which region/country/locale is set for the used user account.

让我们看看发布的代码:

Let us look on posted code:

FOR /F "delims= skip=1" %%i IN ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining') DO (SET /a CHR=%%i)
ECHO "Battery Level: %CHR%"

wmic 的输出例如是表示回车+ Windows 7和Windows XP上的换行符:

The output of wmic is for example with representing carriage return + line-feed on Windows 7 and Windows XP:

EstimatedChargeRemaining  ¶
93                        ¶

此输出重定向到文件中,并以十六进制形式查看:

This output redirected into a file and viewed hexadecimal would look like:

0000h: FF FE 45 00 73 00 74 00 69 00 6D 00 61 00 74 00 ; ÿþE.s.t.i.m.a.t.
0010h: 65 00 64 00 43 00 68 00 61 00 72 00 67 00 65 00 ; e.d.C.h.a.r.g.e.
0020h: 52 00 65 00 6D 00 61 00 69 00 6E 00 69 00 6E 00 ; R.e.m.a.i.n.i.n.
0030h: 67 00 20 00 20 00 0D 00 0A 00 39 00 33 00 20 00 ; g. . .....9.3. .
0040h: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 ;  . . . . . . . .
0050h: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 ;  . . . . . . . .
0060h: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 0D 00 ;  . . . . . . ...
0070h: 0A 00                                           ; ..

格式为:十六进制偏移量:十六进制字节; Windows-1252字符表示形式

The format is: hexadecimal offset: hexadecimal bytes ; Windows-1252 character representation

以上代码导致 wmic 在第一行中跳过第一行输出 FOR 使用环境变量 ComSpec 使用 cmd.exe / C 启动单独的命令过程在后台显示而不显示窗口,并由 FOR 捕获,只包含值名称 EstimatedChargeRemaining

The code above results in skipping the first line output by wmic in a separate command process started by FOR with cmd.exe /C by using environment variable ComSpec in background without displaying an window and captured by FOR containing just the value name EstimatedChargeRemaining.

第二个包含感兴趣的值的行,该值包含一个,两个或三个数字以及21到23个空格(取决于该值的数字),首先分配给循环变量 i ,然后分配给环境变量 CHR 使用算术表达式忽略尾随空格。

The second line containing the value of interest with one, two or three digits with 21 to 23 spaces depending on digits of the value is assigned first to loop variable i and next to environment variable CHR using an arithmetic expression to ignore the trailing spaces.

此代码根据Stephan 上一个问题如何在WMIC结果中获得没有多余空间的价值?在Windows XP上运行完美,但在Windows 7中的错误消息中显示结果:

This code written according to a comment by Stephan on your previous question How to get value without unwanted spaces in WMIC results? works perfect on Windows XP, but results on Windows 7 in the error message:


缺少操作数。

Missing operand.

在Windows XP上,只有一个循环迭代,分配值 93 带有22个尾随空格,并且将回车符返回到环境变量 CHR ,由于该算法,空格和回车符将被命令 SET 忽略

On Windows XP there is just one loop iteration with assigning the value 93 with the 22 trailing spaces and a carriage return to environment variable CHR whereby the spaces and the carriage return are ignored by command SET because of the arithmetic expression.

,但是在Windows 7上,第二次运行循环,只是将回车符分配给循环变量 i 。算术表达式的求值命令 SET 忽略了垂直空格字符回车,这导致第二次循环迭代 set / a CHR = 的执行因此,在语法上正确的算术表达式中,等号后确实存在操作数。

But on Windows 7 the loop is run a second time with just a carriage return assigned to loop variable i. The vertical whitespace character carriage return is ignored by command SET on evaluation of the arithmetic expression which results in execution on second loop iteration set /a CHR= and therefore there is indeed an operand after equal sign missing for a syntactically correct arithmetic expression.

对于 FOR 问题,在解析UTF-时有多种解决方案16个编码输出。

There are multiple solutions possible for this issue of FOR on parsing UTF-16 encoded output.

一个非常简单的输出将环境值分配给感兴趣的值后退出 FOR 循环。

A very easy one is exiting FOR loop after having assigned the value of interest to the environment variable.

for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_Battery GET EstimatedChargeRemaining') do set "CHR=%%I" & goto HaveValue
:HaveValue
echo Battery level: %CHR%%%

所以 > FOR 循环仅迭代一次,并且仅处理带有感兴趣值和尾随空格的 wmic 输出的第二行。选项 delims = 来定义一个空的定界符列表,用于获取 FOR 已删除的尾随空格处理从 wmic 输出捕获的第二行,因此循环变量 I 已经拥有感兴趣的值而没有尾随空格。这样就可以省略 SET 参数 / A 来仅将值分配给环境变量,而无需使用算术表达式。

So the FOR loop iterates just once and processes only the second line of wmic output with the value of interest and the trailing spaces. Option delims= to define an empty list of delimiters is not used to get the trailing spaces already removed by FOR on processing the second line captured from output of wmic and so loop variable I holds the value of interest already without trailing spaces. That makes it possible to omit SET parameter /A to just assign the value to the environment variable without using an arithmetic expression.

要解决由 FOR 输出的 wmic 输出的UTF-16编码行尾错误解析的另一种简单解决方案,正在使用像这样的代码:

Another simple solution to workaround the issue with wrong parsing of UTF-16 encoded line endings of wmic output by FOR is using a code like this:

for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_Battery GET EstimatedChargeRemaining /VALUE') do set "CHR=%%I"
echo Battery level: %CHR%%%

wmic的输出 / VALUE 的code>例如,其中代表回车+换行符:

The output of wmic with option /VALUE is for example with representing carriage return + line-feed:

¶
¶
EstimatedChargeRemaining=93¶
¶
¶

此输出重定向到文件中并以十六进制形式查看:

This output redirected into a file and viewed hexadecimal would look like:

0000h: FF FE 0D 00 0A 00 0D 00 0A 00 45 00 73 00 74 00 ; ÿþ........E.s.t.
0010h: 69 00 6D 00 61 00 74 00 65 00 64 00 43 00 68 00 ; i.m.a.t.e.d.C.h.
0020h: 61 00 72 00 67 00 65 00 52 00 65 00 6D 00 61 00 ; a.r.g.e.R.e.m.a.
0030h: 69 00 6E 00 69 00 6E 00 67 00 3D 00 39 00 33 00 ; i.n.i.n.g.=.9.3.
0040h: 0D 00 0A 00 0D 00 0A 00 0D 00 0A 00             ; ............

因此,开头有两个空行,它们总是被忽略 FOR ,即使使用UTF-16编码也是如此。然后是包含值名称和值之间带有等号的行。最后还有两个空行。

So there are two empty lines at beginning which are always ignored by FOR even on being UTF-16 encoded. Then there is the line containing value name and the value with an equal sign between. Last there are two more empty lines.

两个 FOR 选项 tokens = 2 delims == 结果在等号上分割第三行,并在等号作为循环变量 I 的目标值后仅分配第二个字符串,该变量仅分配给环境变量 CHR 。由于在 wmic 输出末尾解析的空行错误,该命令仅执行一次 SET ,结果仅在一个子字符串中找到了回车符通过 FOR 进行操作,因此它无法为循环变量 I 分配任何内容,因为没有第二个子字符串。因此, FOR 不会第二次执行命令 SET

The two FOR options tokens=2 delims== result in splitting the third line up on equal sign with assigning just second string after the equal sign being the value of interest to loop variable I which is simply assigned next to environment variable CHR. The command SET is executed only once because of the wrong parsed empty lines at end of wmic output result only in one substring with just carriage return being found by FOR and so it cannot assign anything to loop variable I as there is no second substring. For that reason FOR does not execute the command SET a second time.

npocmaka 作为回答上一个问题 >使用第二个 FOR 循环来处理感兴趣的值正确的行。

One more simple solution was posted by npocmaka as answer on your previous question by using a second FOR loop to process the line with the value of interest correct.

其他解决方案使用附加文件或附加命令来输出UTF- 16个编码文本为ASCII编码文本, FOR 可以捕获和处理而没有任何问题。有关此类解决方案,请参见如何在解析输出时更正变量覆盖行为?

Other solutions use an additional file or an additional command to output UTF-16 encoded text as ASCII encoded text which FOR can capture and process without any problems. See answers on How to correct variable overwriting misbehavior when parsing output? for such solutions.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • echo /?

  • 用于/?

  • goto /?

  • set /?

  • wmic /?

  • wmic PATH /?

  • wmic PATH Win32_Battery /?

  • wmic PATH Win32_Battery GET /?

  • echo /?
  • for /?
  • goto /?
  • set /?
  • wmic /?
  • wmic PATH /?
  • wmic PATH Win32_Battery /?
  • wmic PATH Win32_Battery GET /?

这篇关于为什么错误消息“缺少操作数”?在处理WMIC输出并为变量分配值时输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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