【问题标题】:Find all and delete all function for Regedit [closed]查找所有并删除 Regedit 的所有功能 [关闭]
【发布时间】:2014-04-30 17:53:49
【问题描述】:

我正在为 Window 的注册表寻找 全部查找 / 全部删除 功能。我找不到任何可以做到这一点的实用程序。

例如,如果我想删除打印机的所有实例,我必须使用 regedit.exe 中的 F3 和 Del 键删除大约 20 个键/值,这很耗时。

所以我想开发一个小宏或批处理文件(所以无论是 VBA/批处理)来做到这一点。哪个选项更好?那将是一个不错的附加组件,它还能够将已删除的密钥保存在 .reg 文件中。

根据下面的答案,我可以使用reg queryreg delete

我尝试了以下方法:

reg query HKLM /f *myPrinter* /s
reg query HKU /f *myPrinter* /s

这 2 个查询为我提供了我需要删除的所有结果。现在,我如何将其备份到 .reg 密钥文件中并删除 2 个查询的每个结果?

我在 VBA 中这样做更好吗?我不介意为每个查询结果做一个带有列表视图的漂亮 GUI。我想处理循环和所有内容也比在命令行中更容易(假设没有直接的方法可以使用 reg.exe 执行此操作)。

【问题讨论】:

标签: vba batch-file cmd registry


【解决方案1】:
  1. 下载Steve McMahon's cRegistry class
  2. 将一个名为“Registry”的类模块导入到您的项目中。
  3. App.EXEName 的所有实例替换为CurrentProject.Name (原文是为 vb6 编写的。这将允许您在 vba.)
  4. 将以下函数添加到类的末尾。

findSectionKeygetKeyValue 函数实际上实现了这个类,并且是如何使用它的好例子。

Public Function findSectionKey(sectToFind As String, Optional sectToLookIn As String = "") As String
'*****************************************************************************
' Christopher Kuhn 4-16-14
'
' Returns:
'   Full section key as string
'       ex: "software\wow6432Node\ODBC\ODBCINST.INI\Oracle in OraClient11g_home1"
'   If a matching section key is not found, returns an empty string.
'   Only returns first matching section key.
'
' Params:
'       sectToFind - string representing the keynode you're searching for.
'           ex: "ODBCINST.INI"
'       sectToLookIn - String representing the keynode to start the search in.
'           If omitted, use parent reg object's sectionKey value.
'*****************************************************************************
On Error GoTo ErrHandler:
Const PROC_NAME As String = "findSectionKey"

    Dim sSect() As String   ' string array of subnodes
    Dim iSectCount As Long  ' length of sSect array
    Dim reg As Registry     ' use a clone reg so we don't damage current object

    ' Test for optional sectToLookIn param
    If sectToLookIn = "" Then
        sectToLookIn = Me.sectionKey
    End If
    ' create clone
    Set reg = New Registry
    With reg
        .ClassKey = Me.ClassKey
        .sectionKey = sectToLookIn
        ' create array of sections to search
        .EnumerateSections sSect, iSectCount
        ' search each section in array
        Dim i As Long
        For i = 1 To iSectCount
            'Debug.Print .sectionKey & "\" & sSect(i)
            If findSectionKey = "" Then
                If sSect(i) = sectToFind Then
                    ' found node
                    findSectionKey = .sectionKey & "\" & sSect(i)
                    Exit For
                Else
                    'search subnodes via recursion
                    findSectionKey = findSectionKey(sectToFind, .sectionKey & "\" & sSect(i))
                End If
            Else
                Exit For
            End If
        Next i
    End With

ExitFunction:
    If Not (reg Is Nothing) Then
        Set reg = Nothing
    End If
    Exit Function
ErrHandler:
    'errBox CLASS_NAME, PROC_NAME
    Resume ExitFunction
End Function

Public Function getKeyValue(valueKey As String, Optional sectToLookIn As String = "") As Variant
'*****************************************************************************
' Christopher Kuhn 4-16-14
'
' Returns:
'   Value as variant
'   If a matching value key is not found, returns an empty string.
'   Only returns first matching value key.
'
' Params:
'       valueKey - string representing the valueKey you're searching for.
'           ex: "ORACLE_HOME"
'       sectToLookIn - String representing the keynode to start the search in.
'           If omitted, use parent reg object's sectionKey value.
'           If parent reg does not have a sectionKey value, search everywhere.
'*****************************************************************************
On Error GoTo ErrHandler:
Const PROC_NAME As String = "findSectionKey"

    Dim reg As Registry
    Dim sKeys() As String
    Dim iKeyCt As Long
    Dim sSects() As String
    Dim iSectCt As Long
    Dim i As Long
    Dim j As Long

    ' test for optional parameter
    If sectToLookIn = "" And Me.sectionKey <> "" Then
        sectToLookIn = Me.sectionKey
    End If

    ' create reg clone so orginal is not damaged
    Set reg = New Registry
    With reg
        .ClassKey = Me.ClassKey
        If sectToLookIn <> "" Then
            .sectionKey = sectToLookIn
        End If
        ' for each value key in current section
        .EnumerateValues sKeys, iKeyCt
        For i = 1 To iKeyCt
            If sKeys(i) = valueKey Then
                ' found key
                .valueKey = sKeys(i)
                getKeyValue = .value
                Exit For
            End If
        Next i

        ' if key wasn't found, keep looking
        If IsEmpty(getKeyValue) Then
            ' for each section key in current section
            .EnumerateSections sSects, iSectCt
            For j = 1 To iSectCt
                If IsEmpty(getKeyValue) Then
                    ' recursive call
                    If .sectionKey = "" Then
                        ' no section specified
                        getKeyValue = getKeyValue(valueKey, sSects(j))
                    Else
                        ' all other cases
                        getKeyValue = getKeyValue(valueKey, .sectionKey & "\" & sSects(j))
                    End If
                Else
                    ' found key already
                    Exit For
                End If
            Next j
        End If
    End With
ExitFunction:
    If Not (reg Is Nothing) Then
        Set reg = Nothing
    End If
    Exit Function
ErrHandler:
    'errBox CLASS_NAME, PROC_NAME
    Resume ExitFunction
End Function

删除是这样调用的。

Public Sub Delete()
    Dim reg As New Registry
    With reg
        .ClassKey = HKEY_CURRENT_USER
        'delete registry Section key
        .sectionKey = "Software\ODBC\odbc.ini\SomeDataSource"
        If Exists Then
            .DeleteKey
        End If
    End With
End Sub

*我会按原样发布我的整个修改,但它超过了答案中允许的最大字符数。此外,我的注册表扩展并不是删除注册表项所必需的。不过,它们可能会帮助您找到特定键的实例。

【讨论】:

  • 非常感谢您的意见。我实际上正在使用reg queryreg delete 来查看另一个答案,因为它很容易实现(实际上不需要任何真正的编程)但是如果我仍然无法使用命令行,我将仔细研究您的解决方案。
  • @dnLL 我不会争辩说批处理或 .reg 文件不是无限容易实现的。这是。当我需要一个纯 vba 解决方案来解决类似问题时,我想到了这个。祝你好运!
  • 好吧,如果我想要一个 GUI,VBA 可能是要走的路,这看起来是一个可靠的解决方案。
  • 我不得不做一些修改,我对在 VBA 中实现类有点新,但实际上它似乎工作得很好。由于某些原因,我的问题已被搁置,但无论如何我都会接受您的回答。
  • 很明显你上面提到的修改(替换了 APP.EXEName)但是由于某些原因,当行太长时,从这里复制/粘贴代码会产生一些换行符,所以我不得不删除所有那些也是。似乎更像是 SO 的形成错误。到目前为止,除了我没有使用 Steve McMahon 提供的 cls 文件的标头之外,其他的不多,因为我目前正在使用 Access 2010 来生成类文件。这是一个长期项目,因为我可能每天只工作半个小时,但如果有其他事情我可能会更新。
【解决方案2】:
reg query /?

reg delete /?

因此您可以使用查询和构建列表进行搜索,然后使用 delete 进行删除。见

for /?

【讨论】:

  • 谢谢,我正在看。
  • 搜索查询看起来正在工作,但看起来我必须使用删除功能循环遍历结果,不可能只做类似reg delete HKLM /f *pattern* /s
  • 如果它使用两个通配符完成了对注册表的搜索,我将查看输出。与此同时,您可能会发现这个目录很有趣。 c:\Windows\System32\Printing_Admin_Scripts\en-US
猜你喜欢
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2014-12-03
相关资源
最近更新 更多