简体   繁体   English

找到注册表项值的问题

[英]Problems finding the value of registry keys

I'm attempting to make a batch script to uninstall various programs with little involvement from the user (Only saying I want to uninstall Group A (Say, Google Chrome and Microsoft Office)), Partially for work and partially for fun and practice. 我正在尝试制作一个批处理脚本来卸载各种程序,而用户很少参与(只说我要卸载A组(Say,谷歌Chrome和Microsoft Office)),部分用于工作,部分用于娱乐和练习。 This itself isn't impossible, I can already do this by calling msiexec and pointing it to the uninstall location in the registry (msiexec /x {xxxxx-xxxxx-xxxxxx-xxxx}). 这本身并非不可能,我已经可以通过调用msiexec并将其指向注册表中的卸载位置(msiexec / x {xxxxx-xxxxx-xxxxxx-xxxx})来完成此操作。

The only problem with this is that this means that whenever the program changes registry keys, as will often happen with updates, I have to find the path again. 唯一的问题是,这意味着每当程序更改注册表项时,通常会发生更新,我必须再次找到路径。 So, I'm trying to run a piece of code that will search through the HKLM\\SOFTWARE\\Mincrosoft\\Windows\\CurrentVersion\\Uninstall registry location, and whenever the DisplayName returns a certain value, not necessarily exact (Say "Chrome"), it stores the registry location?(The part that would be put in {xxxxxxx-xxxxxxx-xxxxxxx-xxxxxx}). 所以,我正在尝试运行一段代码来搜索HKLM \\ SOFTWARE \\ Mincrosoft \\ Windows \\ CurrentVersion \\ Uninstall注册表位置,并且只要DisplayName返回某个值,不一定完全(说“Chrome”),它存储注册表位置?(将放入{xxxxxxx-xxxxxxx-xxxxxxx-xxxxxx}的部分)。

This way, the program simply finds the programs and stores their locations so that they can be uninstalled with it as a variable. 这样,程序只需查找程序并存储它们的位置,以便可以将它们作为变量卸载。 I have tried, but as far as I know using reg query requires the full location. 我试过,但据我所知,使用reg查询需要完整的位置。 using reg query HKLM /f returns no results. 使用reg查询HKLM / f不返回任何结果。 I've tried a whole range of different workarounds, but nothing seems to work. 我尝试了各种不同的解决方法,但似乎没有任何工作。

Here is a basic version of my code (I am neglecting to post the full version because much of it is repeated. It's mostly just case where's and if then statements) 这是我的代码的基本版本(我忽略了发布完整版本,因为它的大部分内容都是重复的。它主要只是在哪里,如果是,那么语句)

@echo off
:start
echo Hello
echo.
1: Uninstall Chrome

set /p choice="Enter Choice: "
if "%choice%"=="1" goto uninstall_chrome

:uninstall_chrome
reg query HKLM /f Chrome /t REG_SZ
::msiexec /x{uninstall location for the program}

Thanks. 谢谢。

::UPDATE:: ::更新::

I figured it out. 我想到了。 By using some of Rob van der Woude's code, I am now able to do it. 通过使用Rob van der Woude的一些代码,我现在能够做到。 The code to do so is: 这样做的代码是:

CALL :Uninstall "Program Name"

:Uninstall
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=* %%A IN ('REG QUERY HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
REG QUERY "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F DisplayName /V /E 2^>NUL ^| FIND /I " DisplayName "') DO ECHO Program Name = %%C
FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO %%C /qb
)
ENDLOCAL

This will output the programs name and unique identifier, then uninstall the program, without user input ("Do you want to uninstall x?") 这将输出程序名称和唯一标识符,然后卸载程序,无需用户输入(“是否要卸载x?”)

I figured it out. 我想到了。 By using some of Rob van der Woude's code, I am now able to do it. 通过使用Rob van der Woude的一些代码,我现在能够做到。 The code to do so is: 这样做的代码是:

CALL :Uninstall "Program Name"

:Uninstall
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=* %%A IN ('REG QUERY HKLM\SOFWARE\Microsoft\Windows\CurrentVersion\Uninstall /F "%~1" /D /S 2^>NUL ^| FINDSTR /R /B /C:"HKEY_"') DO (
REG QUERY "%%~A" /F DisplayName /V /E | FINDSTR /R /I /C:" DisplayName .* .*%~1" >NUL 2>&1
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F DisplayName /V /E 2^>NUL ^| FIND /I " DisplayName "') DO ECHO Program Name = %%C
FOR /F "tokens=7 delims=\" %%B IN ("%%~A") DO ECHO Unique Identifier = %%B
FOR /F "tokens=2*" %%B IN ('REG QUERY "%%~A" /F UninstallString /V /E ^| FIND /I " UninstallString "') DO %%C /qb
)
ENDLOCAL

This will output the programs name and unique identifier, then uninstall the program, without user input ("Do you want to uninstall x?") 这将输出程序名称和唯一标识符,然后卸载程序,无需用户输入(“是否要卸载x?”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM