& was unexpected at this time

23,873

"& was unexpected at this time" error

There are two mistakes in your batch file.

Mistake 1:

for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%'" get name') do set "DNVER=%%B"

The above command needs to have the single % after NET escaped. Use %% instead:

for /f "skip=1 tokens=4" %%B in ('wmic product where "name like 'Microsoft .NET%%'" get name') do set "DNVER=%%B"

Mistake 2:

for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion' do set "IEFULLVER=%%C"

The above command is missing a ) before do. It should be:

for /f "tokens=3" %%C in ('reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v svcVersion') do set "IEFULLVER=%%C"

Working batch file (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

Example output:

F:\test>test
64-bit
4.5.2
11.0.9600.17691

Escaping Percents

The % character has a special meaning for command line parameters and FOR parameters.

To treat a percent as a regular character, double it:

%%

Source syntax


Further Reading

Share:
23,873

Related videos on Youtube

MarkCrawford
Author by

MarkCrawford

Updated on September 18, 2022

Comments

  • MarkCrawford
    MarkCrawford 3 months

    I have a batch file that is supposed to set some variables at the beginning.

    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%
    

    If I run these in CMD prompt, (substituting %%A for %A etc), they work as expected, but when I run the batch file, the first line runs as expected and then I get an & was unexpected at this time error. I'm thinking it might be to do with the % after .NET, but it has to be there otherwise the wmi query won't return the value I need.

    • TOOGAM
      TOOGAM over 6 years
      Why is the third line missing a right parenthesis? What happens if, after the first line, you add: ECHO %OSARCH%, and after the current second line, you add, ECHO %DNVER%, and after the current third line, you add, ECHO %IEFULLVER%, and after the current 4th line, ECHO %IEVER%? (Even if you still get the error, it may be nice to see where you get the error, compared to some of the other output that may exist.)
    • MarkCrawford
      MarkCrawford over 6 years
      It's missing the parenthesis because I'm a plank and didnt notice the mistake!
  • MarkCrawford
    MarkCrawford over 6 years
    Hi David, Thank you for your answer, fixing the mistakes have worked perfectly. After reading about FOR on SS64 several times last night, it just didn't sink in that doubling the % character applied to the string as well as the parameters.