reg根据另一个键值查询键值

时间:2013-04-11 19:37:42

标签: batch-file

我要做的是让批处理脚本返回程序的卸载链接。所以基本上我想要这样的东西:

Select UninstallString from HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall where DisplayName='Sublime Text 1.0"

我正在使用

reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /S^|find " DisplayName"

最初得到一个程序列表,然后放入一个菜单,然后我选择要卸载的程序,它应该转到该程序的HKLM \ SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall中的注册表得到它的UninstallString值

2 个答案:

答案 0 :(得分:1)

试试这个:

@echo off&setlocal enabledelayedexpansion
set "regroot=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
set "file=%~dpn0.txt"

set /a fcount=0
for /f "delims==" %%i in ('set $ 2^>nul') do set "%%i="
if exist "%file%" (
    for /f "usebackqtokens=1*delims=|" %%i in ("%file%") do (
        set /a fcount+=1
        set "$d%%j=%%j"
        set "$u%%j=%%i"
    )
    goto:menu
)
echo(building "%file%", please wait
for /f "delims=" %%i in ('reg query "%regroot%"') do (
    set "DN="& set "US="
    for /f "tokens=2*" %%j in ('reg query "%regroot%\%%~ni" /v DisplayName 2^>nul^|find /i "DisplayName"') do set "DN=%%~k"
    for /f "tokens=2*" %%j in ('reg query "%regroot%\%%~ni" /v UninstallString 2^>nul^|find /i "UninstallString"') do set "US=%%~k"
    if not "!DN!"=="" if not "!US!"=="" if not defined $d!DN! (
        >>"%file%" echo(!US!^|!DN!
        set /a fcount+=1
        set "$d!DN!=!DN!"
        set "$u!DN!=!US!"
        <nul set/p"=."
    )
)
echo(
:menu
echo(%fcount% programs with uninstall strings found.
:loop
set /a pcount=0
set "program="
set /p "program=type a program name (q=quit): "
if not defined program goto:loop
if "%program%"=="q" goto:eof
echo(
for /f "tokens=2delims==" %%i in ('set $d ^|findstr !program! 2^>nul') do (
    echo(%%i
    echo(!$u%%i!
    echo(
    set /a pcount+=1
)
if %pcount% equ 0 (echo(!program! not found.) else echo(%pcount% program(s^) found.
goto:loop

您可以使用findstr的一些REGEX功能(例如。/i进行不区分大小写的搜索)。请注意:要搜索以“M”开头的所有程序,您可以使用"^$dM"/b $dM。可搜索的字符串始终是前导$d

答案 1 :(得分:0)

试试这个“双线”:

for /f "tokens=7 delims=\" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /S /F "MySoftware" ^| find "{"') do set ProgramUninstallRegKey=%%a
for /f "skip=1 tokens=3*" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%ProgramUninstallRegKey% /V "UninstallString"') do set ProgramUninstallString=%%a %%b