I want to delete several registry keys. I know how to delete one reg key with subkeys but I don't know how to add more reg keys with subkeys to delete.

Can you please help me?

Here is my code for one key with subkeys to delete. Works perfectly... How to add more keys in HKEY_CURRENT_USER\Software\Test, HKEY_CURRENT_USER\Software\Test1, HKEY_CURRENT_USER\Software\Test2.... to delete?

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

Const COMPUTER = "."

Sub DeleteKey(Hive, KeyPath)
Dim ValueArray, Value

' List subkeys
objReg.EnumKey Hive, KeyPath, ValueArray

If Not IsNull(ValueArray) Then
For Each Value In ValueArray
' Starts recursion
DeleteKey Hive, KeyPath + "\" + Value
Next
End If
' Deletes the key
objReg.DeleteKey Hive, KeyPath + "\" + Value
End Sub

Dim KeyPath
Dim objReg

Set objReg = _
GetObject("winmgmts:\\" &_
COMPUTER & "\root\default:StdRegProv")

KeyPath = "Software\Test"

DeleteKey HKEY_CURRENT_USER, KeyPath
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Just call the sub function with different arguments again.

You have the sub being called with:

DeleteKey HKEY_CURRENT_USER, KeyPath

Keypath being assigned as "Software\Test." To run this multiple times, just call the sub again with the keypath argument replaced. For simplicity's sake, you could do this to delete the key HKCU\Software\test2:

DeleteKey HKEY_CURRENT_USER, "Software\Test2"

You can call the sub as many times as you like in this fashion. There are more powerful ways to automate this to use lists, but I believe just calling the sub for each key you want to delete in this manner should get you where you need to be.
Answered 08/12/2009 by: Jsaylor
Second Degree Blue Belt

Please log in to comment
0
I was too late posting my new code. You have been a bit faster...

Here is my new code and it works.

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

Const COMPUTER = "."

Sub DeleteKey(Hive, KeyPath)
Dim ValueArray, Value

' List subkeys
objReg.EnumKey Hive, KeyPath, ValueArray

If Not IsNull(ValueArray) Then
For Each Value In ValueArray
' Starts recursion
DeleteKey Hive, KeyPath + "\" + Value
Next
End If
' Deletes the key
objReg.DeleteKey Hive, KeyPath + "\" + Value
End Sub

Dim objReg

Set objReg = _
GetObject("winmgmts:\\" &_
COMPUTER & "\root\default:StdRegProv")

DeleteKey HKEY_CURRENT_USER, "Software\Test"
DeleteKey HKEY_CURRENT_USER, "Software\Test1"
DeleteKey HKEY_CURRENT_USER, "Software\Test2"
DeleteKey HKEY_CURRENT_USER, "Software\Test3"
DeleteKey HKEY_CURRENT_USER, "Software\Test4"


Nevertheless, thanks for help


EDIT: The reason for it, I need this in order to get a clean state of the machines on Windows 7. Windows Installer remains several keys after uninstalling. It's all marked to delete the keys on uninstall in the tables but anyhow windows installer leaves them on the machine on Windos 7. And I guess the same happens on VISTA machines...
Answered 08/12/2009 by: McRip
Orange Senior Belt

Please log in to comment
0
...or go completely mad and use the RemoveRegistry table.

BTW, if you want to persist with registry-handling via script, get hold of the cRegistry class from JSWare (http://www.jsware.net) IIRC, it's part of the class pack there. I use an edited version of this class for any script which requires access to the registry and it has proved its worth over and over, not the least of which is the time-saver of not having to create the same functions ad infinitum. There's an INI handling class there, too. The one feature common to both is a means to enumerate keys (or sections in the INI class), values and values with their data. That alone makes it head and shoulders above the brain-dead registry scripts you'll find knocking around.

Don't be put off by the scary word 'class'. Using it is simple and usage is explained in the file itself.
Answered 08/13/2009 by: VBScab
Red Belt

Please log in to comment
0
ORIGINAL: VBScab

...or go completely mad and use the RemoveRegistry table.



I did use the RemoveRegistry table but it did not work correctly on Windows 7 machines, that's the point. I was testing around with my VBScript and it works perfectly. Now the Windows 7 machine is in a clean state after uninstalling...

But I will adopt your proposal for (http://www.jsware.net). I'm gonna study this one for a while and will then make my choice.

Thank you
Answered 08/13/2009 by: McRip
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity