&此时是出乎意料的

&此时是出乎意料的

我有一个批处理文件,应该在开始时设置一些变量。

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 参数具有特殊含义。

要将百分比视为常规字符,请将其加倍:

%%

来源句法


进一步阅读

相关内容