Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through Registry Subfolders

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.

like image 357
Dennis Kriechel Avatar asked Aug 07 '13 08:08

Dennis Kriechel


2 Answers

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
like image 168
Ansgar Wiechers Avatar answered Nov 17 '22 01:11

Ansgar Wiechers


In addition i found a realy good example on the web:

' Constants (taken from WinReg.h)
'
Const HKEY_CLASSES_ROOT   = &H80000000
Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003

Const REG_SZ        = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY    = 3
Const REG_DWORD     = 4
Const REG_MULTI_SZ  = 7

' Chose computer name, registry tree and key path
'
strComputer = "." ' Use . for current machine
hDefKey = HKEY_LOCAL_MACHINE
strKeyPath = "SOFTWARE\Microsoft\Cryptography\Defaults\Provider"

' Connect to registry provider on target machine with current user
'
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

' Enum the subkeys of the key path we've chosen
'
oReg.EnumKey hDefKey, strKeyPath, arrSubKeys

For Each strSubkey In arrSubKeys

  ' Show the subkey
  '
  wscript.echo strSubkey

  ' Show its value names and types
  '
  strSubKeyPath = strKeyPath & "\" & strSubkey
  oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes

  For i = LBound(arrValueNames) To UBound(arrValueNames)
    strValueName = arrValueNames(i)
    Select Case arrTypes(i)

      ' Show a REG_SZ value
      '
      Case REG_SZ          
        oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue
        wscript.echo "  " & strValueName & " (REG_SZ) = " & strValue

      ' Show a REG_EXPAND_SZ value
      '
      Case REG_EXPAND_SZ
        oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue
        wscript.echo "  " & strValueName & " (REG_EXPAND_SZ) = " & strValue

      ' Show a REG_BINARY value
      '          
      Case REG_BINARY
        oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes
        strBytes = ""
        For Each uByte in arrBytes
          strBytes = strBytes & Hex(uByte) & " "
        Next
        wscript.echo "  " & strValueName & " (REG_BINARY) = " & strBytes

      ' Show a REG_DWORD value
      '
      Case REG_DWORD
        oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue
        wscript.echo "  " & strValueName & " (REG_DWORD) = " & CStr(uValue)               

      ' Show a REG_MULTI_SZ value
      '
      Case REG_MULTI_SZ
        oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues                                
        wscript.echo "  " & strValueName & " (REG_MULTI_SZ) ="
        For Each strValue in arrValues
          wscript.echo "    " & strValue 
        Next

    End Select
  Next

Next
like image 36
Dennis Kriechel Avatar answered Nov 17 '22 03:11

Dennis Kriechel