Search code examples
batch-fileregistry

REG QUERY combined with FOR return Half of what I need


I have a registry key created by InnoSetup during installation. This key contains two information :

  • DisplayName
  • QuietUninstallString

I don't know what is the full name of my Key. The only informations I have is the path :

"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

and the value of the key "DisplayName" (MyApplicationName).

What I need to do, is to retrieve the "QuietUninstallString" value, which is supposed to be a path with "/SILENT" at the end of the path.

Right now I wrote this batch file :

@echo off
setlocal enableextensions disabledelayedexpansion
set tosearch=%1

for /f "tokens=3" %%a in ('
    REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /s /f %tosearch% ^| findstr "QuietUninstallString"
') do set "value=%%a"

echo %value%

And I call it like this :

test.bat MyApplicationName

Problem is, when I run this file, it return :

"C:\Program

While I want this to be returned :

"C:\Program Files (x86)\MyFolder\MyApplicationName\unins000.exe" /SILENT

Is there a better way to retrieve the information with my batch file ?


Solution

  • Based upon what you've posted, you should be able to replace your entire code with this:

    @For /F "EOL= Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query
     "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" /S /F
     "%~1" /E /D 2^>NUL') Do @For /F "Tokens=2,*" %%H In (
        '%SystemRoot%\System32\reg.exe Query "%%G" /V "QuietUninstallstring" 2^>NUL'
    ) Do @Echo(%%I
    

    Or if the intention is to actually run the uninstall string:

    @For /F "EOL= Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query
     "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" /S /F
     "%~1" /E /D 2^>NUL') Do @For /F "Tokens=2,*" %%H In (
        '%SystemRoot%\System32\reg.exe Query "%%G" /V "QuietUninstallstring" 2^>NUL'
    ) Do @Start "" %%I