I am trying to query the install location of a program in the registry. All I'm interested in is the location output.
This question has a partial solution, but it doesn't quite help. On Windows 7, the reg command outputs a stupid registry key header along with the value, as shown below:
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode
InstallLocation REG_EXPAND_SZ C:\Program Files\NSIS
First, is there a way to turn off the header and simplify the output?
At the command prompt, I can change the above to
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation | findstr InstallLocation
so that it returns me just the second line.
Now, if I am to use a FOR /F to parse this and get only the directory value, the FOR command fails saying | was unexpected at this time.
Here's my batch file:
@for /f "tokens=2* delims= " %%k in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation | findstr InstallLocation') do @echo %%k
So where am I going wrong?
You must escape the | character using a caret (^).
@echo off
setlocal
set KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode
set V=InstallLocation
for /f "tokens=2* delims= " %%k in ('reg query "%KEY%" /v %V% ^| findstr "%V%"') do echo %%k
this would return REG_SZ on my machine.
The pipe char is special and has to be escaped with ^.
@for /f "tokens=2* delims= " %%k in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation ^| findstr InstallLocation') do @echo %%k
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With