Does a registry key exist?

Discussion in 'Scripting' started by Len Cook, Jul 11, 2003.

  1. Len Cook

    Len Cook Guest

    Several resources provide info on reading, writing, and
    deleting registry values and keys. So far I haven't seen
    how to check to see if a registry key EXISTS. There is,
    of course, an EXIST for files, folders.
    I can enumerate the subkeys into an array, then perform
    substring matches against the array elements, but that
    seems ugly.

    Thanks,
    Len
     
    Len Cook, Jul 11, 2003
    #1
    1. Advertisements

  2. 'Create WBEM Interface to make system Queries
    Set oLoc = CreateObject("WbemScripting.SWbemLocator")

    'Connect to system via WBEM
    StrServer = "."
    Set oSvcs = oLoc.ConnectServer(StrServer, "Root\Default")

    'Get a class Instance
    Set oObjs = oSvcs.get("StdRegProv") 'Get a class Instance

    'Define arguments
    HKLM = &h80000002
    sKeyName = "Software\Microsoft\Windows NT\CurrentVersion"

    'To read a key's default unnamed value rather than a
    'named value's value, use: sValueName = ""

    sValueName = "RegisteredOwner"

    'Get the value
    rc = oObjs.GetStringValue(HKLM, sKeyName, sValueName, sValue)

    'Check for error

    'RC = 0 for success
    'RC = 1 for key read with no default value
    'RC = 2 for key not found
    'RC = 5 for permission denied
    'RC = 6 for invalid hive

    if rc = 0 then
    msgbox sValue
    else
    msgbox "rc=" & rc
    end if

    --
    Michael Harris
    Microsoft.MVP.Scripting
    Seattle WA US

    Technet Script Center
    http://www.microsoft.com/technet/scriptcenter/default.asp

    Microsoft® Windows®2000 Scripting Guide
    http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
     
    Michael Harris \(MVP\), Jul 11, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.