Vba “查找全部”和“删除全部”函数用于Regedit
我正在为Window的注册表寻找“全部查找/全部删除”功能。我找不到任何实用程序可以做到这一点 例如,如果我想删除打印机的所有实例,我必须使用regedit.exe中的F3和Del key删除大约20个键/值,这非常耗时 因此,我想开发一个小的宏或批处理文件(所以VBA/批处理)这样做。哪种选择更好?如果能够将删除的密钥保存在.reg文件中,那将是一个不错的附加组件 根据下面的回答,我可以使用Vba “查找全部”和“删除全部”函数用于Regedit,vba,batch-file,cmd,registry,Vba,Batch File,Cmd,Registry,我正在为Window的注册表寻找“全部查找/全部删除”功能。我找不到任何实用程序可以做到这一点 例如,如果我想删除打印机的所有实例,我必须使用regedit.exe中的F3和Del key删除大约20个键/值,这非常耗时 因此,我想开发一个小的宏或批处理文件(所以VBA/批处理)这样做。哪种选择更好?如果能够将删除的密钥保存在.reg文件中,那将是一个不错的附加组件 根据下面的回答,我可以使用reg query和reg delete 我尝试了以下方法: reg query HKLM /f *my
reg query
和reg delete
我尝试了以下方法:
reg query HKLM /f *myPrinter* /s
reg query HKU /f *myPrinter* /s
这两个查询提供了我需要删除的所有结果。现在,我如何将其备份到.reg密钥文件中并删除这两个查询的所有结果
我在VBA中这样做更好吗?我不介意为每个查询结果使用一个带有列表视图的漂亮GUI。我想处理循环和所有事情也比在命令行中更容易(假设使用reg.exe没有直接的方法)
及
因此,您可以使用查询和生成列表进行搜索,然后使用“删除”进行删除。看
for /?
App.EXEName
的所有实例替换为CurrentProject.Name
(原始版本是为vb6编写的。这将允许您在中使用它。)
vba。)findSectionKey
和getKeyValue
实际上实现了这个类,是如何使用它的好例子
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
*我本来会按原样发布我的全部修改,但它超出了答案中允许的最大字符数。此外,删除注册表项并不一定需要我的注册表扩展。不过,它们可能会帮助您找到特定键的实例。从这里开始:@TimWilliams我已经找到了,SU的第二个结果仍然没有回答。谢谢,我现在正在查看。搜索查询看起来正在工作,但我似乎必须使用delete函数循环搜索结果,不可能只执行类似于
reg delete HKLM/f*pattern*/s的操作,如果它使用两个通配符搜索完注册表,我将查看输出。同时,你可能会发现这个目录很有趣。c:\Windows\System32\Printing\u Admin\u Scripts\n我们非常感谢您的输入。实际上,我正在使用reg query
和reg delete
查看另一个答案,因为它非常容易实现(实际上不需要任何真正的编程),但是如果我仍然无法使用命令行执行,我会仔细看看您的解决方案。@dnLL我不会争辩批处理或.reg文件的实现不是非常容易。它是。当我需要一个类似问题的纯vba解决方案时,我想到了这个。祝你好运如果我想要一个图形用户界面,VBA可能是一个不错的选择,这看起来是一个很好的解决方案。我做了一些修改,我对VBA中类的实现有点陌生,但实际上它似乎工作得很好。由于某些原因,我的问题被搁置,但无论如何我都会接受你的答案。很明显,你在上面提到的修改(替换了APP.EXEName),但由于某些原因,当代码行太长时,从这里复制/粘贴代码会产生一些换行符,因此我也不得不删除所有这些换行符。看起来更像是SO的一个格式化错误。到目前为止,除了我没有使用Steve McMahon提供的cls文件的头之外,没有其他的内容,因为我正在使用Access 2010生成类文件。这是一个长期的项目,因为我可能每天只工作半个小时,但如果有其他事情,我可能会更新这个。
for /?
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