Iterate through Registry Subfolders

Dennis Kriechel picture Dennis Kriechel · Aug 7, 2013 · Viewed 20.8k times · Source

I want to get all values of a registry path include the values of its subfolders. Right now i read the values of a single folder by this:

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
strComputer & "\root\default:StdRegProv")

strKeyPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
    msgbox subkey ' Just for debugging
Next

This works great, but in addition i need to get a list of the folder's subfolders.

I want to get a result (only the content is important, not the formatting and no need to write it into a file) like the this would command gives me:

regedit /e c:\testfile.reg   
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

Is there a way to do this in vbs or do i need to use the regedit command from windows, with an Wscript.Shell call.

Answer

Ansgar Wiechers picture Ansgar Wiechers · Aug 7, 2013

You need to recurse into the subkeys. Try this:

Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

Sub EnumerateKeys(hive, key)
  WScript.Echo key
  reg.EnumKey hive, key, arrSubKeys
  If Not IsNull(arrSubKeys) Then
    For Each subkey In arrSubKeys
      EnumerateKeys hive, key & "\" & subkey
    Next
  End If
End Sub

Set reg = GetObject("winmgmts://./root/default:StdRegProv")

EnumerateKeys HKEY_LOCAL_MACHINE, strKeyPath