Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using piped commands with for /f on windows (with reg query)

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?

like image 535
Rex Avatar asked Oct 22 '25 15:10

Rex


2 Answers

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.

like image 145
Tomalak Avatar answered Oct 24 '25 08:10

Tomalak


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
like image 24
Matej Avatar answered Oct 24 '25 08:10

Matej