2014-04-30 57 views
1

我在寻找一个找到全部/全部删除函数为Window的注册表。我找不到任何这样做的实用工具。查找所有和删除所有功能的Regedit

例如,如果我想删除打印机的所有实例,我必须使用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来做到这一点)。

+0

从这里开始:http://www.google.com/webhp?nord=1#nord=1&q=registry+find+delete –

+0

@TimWilliams我已经做了,SU上的第二个结果仍然没有答案。 – dnLL

回答

1
  1. 下载Steve McMahon's cRegistry class
  2. 将它导入到您的项目中名为“Registry”的类模块。
  3. CurrentProject.Name 替换的App.EXEName所有实例(原来是为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 

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

+0

非常感谢您的意见。我实际上是用'reg query'和'reg delete'查看另一个答案,因为它很容易实现(实际上并不需要任何真正的程序化),但是如果我仍然无法使用命令行,我会仔细看看你的解决方案。 – dnLL

+0

@dnLL我不会认为批处理或.reg文件不是无限容易实现的。它是。当我需要一个纯粹的vba解决方案来解决类似的问题时,我提出了这个问题。祝你好运! – RubberDuck

+0

好吧,如果我想要一个GUI,VBA可能是要走的路,这看起来像是一个可靠的解决方案。 – dnLL

1
reg query /? 

reg delete /? 

所以,你可以查询搜索和创建列表,然后使用delete删除。见

for /? 
+0

谢谢,我正在看它。 – dnLL

+0

该搜索查询看起来工作,但它看起来像我必须通过删除功能循环的结果,没有可能做一些像'reg删除HKLM/f * pattern */s' – dnLL

+0

如果它完成搜索注册与两个通配符我会看看输出。同时你可能会觉得这个目录很有趣。 c:\ Windows \ System32 \ Printing_Admin_Scripts \ en-US –