Jump to content

Help with for /f 'reg query'


Recommended Posts

Guest XhmikosR
Posted

Hello, everyone. I did a search and found many useful things but I couldn't find an entire solution for what I want to do.

Anyway I want to retrieve a registry value and set it as a variable to use it later. Example:

Key: HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Inno Setup 5_is1

String: Inno Setup: App Path or InstallLocation (I don't know if there is a difference)

So I want to do something like this:

FOR /f "tokens=3 delims=	" %%i in (
'reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Inno Setup 5_is1" /v "Inno Setup: App Path"'
) DO (
SET SetupPath=%%i
)

But I probably do something wrong with the tokens and/or delims.

Also how could I check first if a specific executable is in path and if not then check for the above registry key?

Thanks everyone in advance!


Posted

I thought I'd provide an example which would work more like a template for any Inno Setup Application.

Line 5 below is already set for the Topic Starter's app, anyone else can set it accordingly. You'd do this by replacing between the = and " with the Inno Setup Application Name minus the trailing _is1:

e.g. Set "A_=Wise Registry Cleaner" would read the "Wise Registry Cleaner_is1" Uninstall Key

@Echo off&Setlocal enableextensions
Set "U_=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Set "I_=Inno Setup"

Set "A_=%I_% 5"

For /f "delims=" %%a In (
'Reg query "%U_%\%A_%_is1" /v "%I_%: App Path"^|Find "REG_"') Do (
Set "InnoAppPath=%%a"&Call :Sub %%InnoAppPath:*Z=%%)

Echo:%InnoAppPath%

Pause&Goto :Eof

:Sub
Set InnoAppPath="%*"

The code has been changed from the usual delims=<tab> method to make it compatible with Vista too. The resulting variable, %InnoAppPath%, will be surrounded by double quotes, if this is not a requirement in your case you can remove them on the last line of this code.

Guest XhmikosR
Posted
I thought I'd provide an example which would work more like a template for any Inno Setup Application.

Line 5 below is already set for the Topic Starter's app, anyone else can set it accordingly. You'd do this by replacing between the = and " with the Inno Setup Application Name minus the trailing _is1:

e.g. Set "A_=Wise Registry Cleaner" would read the "Wise Registry Cleaner_is1" Uninstall Key

@Echo off&Setlocal enableextensions
Set "U_=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Set "I_=Inno Setup"

Set "A_=%I_% 5"

For /f "delims=" %%a In (
'Reg query "%U_%\%A_%_is1" /v "%I_%: App Path"^|Find "REG_"') Do (
Set "InnoAppPath=%%a"&Call :Sub %%InnoAppPath:*Z=%%)

Echo:%InnoAppPath%

Pause&Goto :Eof

:Sub
Set InnoAppPath="%*"

The code has been changed from the usual delims=<tab> method to make it compatible with Vista too. The resulting variable, %InnoAppPath%, will be surrounded by double quotes, if this is not a requirement in your case you can remove them on the last line of this code.

Thank you very much! It does work for every Inno Setup App, on XP and on Vista.

Since you seem to be very familiar with batch scripting, I'd like to ask you some more questions I have. :angel

1) How can I do the above you suggested, but if there is no registry key (the app is not installed) do not show any messages on the screen but echo a custom message and go to a further step in the script?

2) How can I first check if an app is in PATH? Again without any output on screen and if is in PATH do something, if not do something else.

Thank you again, in advance!:)

Posted

1.

@Echo off&Setlocal enableextensions
Set "U_=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Set "I_=Inno Setup"

Set "A_=%I_% 5"
Set "M_=This is a custom message"

For /f "delims=" %%a In (
'Reg query "%U_%\%A_%_is1" /v "%I_%: App Path"2^>Nul^|Find "REG_"') Do (
Set "InnoAppPath=%%a"&Call :Sub %%InnoAppPath:*Z=%%)

If not defined InnoAppPath Echo:%M_%&&Goto :FurtherStep

Echo:%InnoAppPath%
Pause&Goto :Eof

:FurtherStep
Echo:This is a further step

Pause&Goto :Eof

:Sub
Set InnoAppPath="%*"

2.

@Echo off&Setlocal enableextensions

Set "A_=reg.exe"

For %%# In (%A_%) Do If %%~$PATH:#' Equ ' (Goto DoOneThing)
Echo:The app is in PATH
Goto :Eof
:DoOneThing
Echo:The app is not in PATH

Guest XhmikosR
Posted

You are the master! Thank you very much for everything.:D

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...