Filtering registry UninstallString dependant on drive

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 10 Aug 2018 05:36

Filtering registry UninstallString dependant on drive

#1 Post by its_JP » 10 Aug 2018 06:42

Hi all,

Put bluntly I'm new to batch. I have little to no experience with it and I have been tasked with helping to automate the upgrade/uninstall process for Java.
The script below is a modified version of one found here ( ... 3#33279973), which initially worked as intended.

Code: Select all

@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%b in ('
      reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall ^
          /s /d /f "Java" ^| findstr "HKEY_ DisplayName"
  ') do (
      set "line=%%b"
      if "!line:~0,4!"=="HKEY" (
          set "key=!line!"
      ) else (
          set Uninstall=
          rem Sort /r makes QuietUninstallString the last line
          for /f "tokens=2*" %%c in ('
              reg query "!key!" ^| find "UninstallString" ^| sort /r
          ') do if not "%%d"=="" set "Uninstall=%%d"

          if defined Uninstall (
              for /f "tokens=2*" %%c in ("!line!") do echo Found %%d
              echo Running !Uninstall! (COMMENTED FOR TESTING)
              rem call !Uninstall!

However after complications involving multiple installs, the requirements have changed, and I have very little idea on how to modify the script to meet these changes. I need to ensure this only pulls (and subsequently calls) Java uninstall strings from the 'D:\' Drive. For reference there are two other drives with varying versions of Java installed that need to remain untouched.

My thought process to achieve this is to add another |findstr, searching the InstallLocation part of the key for "D:\" - I have attempted to implement this with minor success. Help achieving this would be appreciated.

Posts: 492
Joined: 21 Mar 2014 08:50

Re: Filtering registry UninstallString dependant on drive

#2 Post by Compo » 12 Aug 2018 08:48

Here's an untested example script which may output the required uninstall key.

It is based off the information you've provided, except for one assumption, that being the string "Java" will be contained within the same REG_SZ string as "D:\"

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "reg=%__AppDir__%reg.exe"
Set "key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

Set "rid="
For /F "EOL= Delims=" %%A In (
	'""%reg%" Query "%key%" /S /F "Java" /D 2>Nul|Find "_""'
) Do "%reg%" Query "%%A" /V "InstallLocation" 2>Nul|FindStr/I "D:\\.*Java">Nul&&(
	For /F "EOL=H Tokens=2*" %%B In (
		'""%reg%" Query "%%A" /F "UninstallString" /V 2>Nul|Find "_"|Sort /R"'
	) Do Set "rid=%%C")
If Defined rid Echo=%rid%
If you're happy with the output, and wish to actually perform the uninstall, remove Echo= from the penultimate line and optionally delete the last line.

Post Reply