2014-04-30 54 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 –