0

I am currently trying to make a batch script that finds if a program is installed on the given machine, and if it is it outputs it to a file. But, when I attempt at getting the string, I get a "| was unexpected at this time" error.

wmic product get name > C:\Users\%USERNAME%\Desktop\programswmic.txt
for /F "tokens=*" %%A in ('type C:\Users\%USERNAME%\Desktop\programswmic.txt | findstr /m "iTunes"') do (set var=%%A)
echo %var% > C:\Users\%USERNAME%\Desktop\endingoutput.txt

The output of the script is below:

C:\Users\Landon\Desktop>test.bat

C:\Users\Landon\Desktop>wmic product get name  1>C:\Users\Landon\Desktop\programswmic.txt
| was unexpected at this time.

C:\Users\Landon\Desktop>for /F "tokens=*" %A in ('type C:\Users\Landon\Desktop\programswmic.txt | findstr /m "iTunes"') do (set var=%A)

C:\Users\Landon\Desktop>

How do I fix this?

LandonBB
  • 5
  • 3

1 Answers1

0

You need to escape the | character using a ^.

wmic product get name > C:\Users\%USERNAME%\Desktop\programswmic.txt
for /F "tokens=*" %%A in ('type C:\Users\%USERNAME%\Desktop\programswmic.txt^| findstr /m "iTunes"') do (set var=%%A)
echo %var% > C:\Users\%USERNAME%\Desktop\endingoutput.txt
John Kens
  • 1,615
  • 2
  • 10
  • 28