![&此时是出乎意料的](https://linux22.com/image/1497097/%26%E6%AD%A4%E6%97%B6%E6%98%AF%E5%87%BA%E4%B9%8E%E6%84%8F%E6%96%99%E7%9A%84.png)
我有一个批处理文件,应该在开始时设置一些变量。
for /f %%A in ('wmic os get osarchitecture ^| find /i "bit"') do set "OSARCH=%%A"
for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%'" get name') do set "DNVER=%%B"
for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion' do set "IEFULLVER=%%C"
set IEVER=%IEFULLVER:~0,4%
如果我在 CMD 提示符下运行这些命令(用 %%A 替换 %A 等),它们会按预期工作,但是当我运行批处理文件时,第一行会按预期运行,然后出现错误。我想这可能与after& was unexpected at this time
有关,但它必须在那里,否则 wmi 查询将不会返回我需要的值。%
.NET
答案1
“&此时是意外的”错误
您的批处理文件中有两个错误。
错误1:
for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%'" get name') do set "DNVER=%%B"
上述命令需要将后面的单数%
转义NET
。请使用%%
:
for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%%'" get name') do set "DNVER=%%B"
错误2:
for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion' do set "IEFULLVER=%%C"
上述命令缺少一个)
。do
它应该是:
for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion') do set "IEFULLVER=%%C"
工作批处理文件(test.cmd):
@echo off
setlocal EnableDelayedExpansion
for /f %%A in ('wmic os get osarchitecture ^| find /i "bit"') do set "OSARCH=%%A"
for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%%'" get name') do set "DNVER=%%B"
for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion') do set "IEFULLVER=%%C"
echo %OSARCH%
echo %DNVER%
echo %IEFULLVER%
endlocal
示例输出:
F:\test>test
64-bit
4.5.2
11.0.9600.17691
转义百分号
该
%
字符对于命令行参数和 FOR 参数具有特殊含义。要将百分比视为常规字符,请将其加倍:
%%
来源句法
进一步阅读
- Windows CMD 命令行的 AZ 索引- 与 Windows cmd 行相关的所有事物的绝佳参考。
- 句法- 转义字符、分隔符和引号。
- 西米克- Windows 管理规范命令。