How do you check is a registry key exist or not in VBScript/WSH? 
Author Message
 How do you check is a registry key exist or not in VBScript/WSH?

Try this function, passing it the key to be tested. The trailing backslash is optional.

Function KeyExists(ByVal RegKey)
    Dim Shell
    Dim ErrDesc(1)
    Set Shell = CreateObject("WScript.Shell")
    KeyExists = True
    On Error Resume Next
    RegKey = Replace(RegKey & "\","\\","\")
    Shell.RegRead(vbcr)
    ErrDesc(0) = Replace(Err.Description,vbcr,"")
    Err.Clear
    Shell.RegRead(RegKey)
    ErrDesc(1) = Replace(Err.Description,RegKey,"")
    If ErrDesc(0) = ErrDesc(1) Then KeyExists = False
End Function


| I need to check and see if a key exist or not and then
| do a procedure if true or false
|
| Key: ("HKLM\Software\Microsoft\Windows\CurrentVersion\Version")
| Key: ("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
|
|
|
|



Tue, 02 Sep 2003 07:22:39 GMT  
 How do you check is a registry key exist or not in VBScript/WSH?
OK,
How do you find out if a Value Key exist or not?
("HKLM\Software\Microsoft\Windows NT\CurrentVersion\")

Value Key = Currentversion



Quote:
> Try this function, passing it the key to be tested. The trailing backslash
is optional.

> Function KeyExists(ByVal RegKey)
>     Dim Shell
>     Dim ErrDesc(1)
>     Set Shell = CreateObject("WScript.Shell")
>     KeyExists = True
>     On Error Resume Next
>     RegKey = Replace(RegKey & "\","\\","\")
>     Shell.RegRead(vbcr)
>     ErrDesc(0) = Replace(Err.Description,vbcr,"")
>     Err.Clear
>     Shell.RegRead(RegKey)
>     ErrDesc(1) = Replace(Err.Description,RegKey,"")
>     If ErrDesc(0) = ErrDesc(1) Then KeyExists = False
> End Function



> | I need to check and see if a key exist or not and then
> | do a procedure if true or false
> |
> | Key: ("HKLM\Software\Microsoft\Windows\CurrentVersion\Version")
> | Key: ("HKLM\Software\Microsoft\Windows

NT\CurrentVersion\CurrentVersion")

- Show quoted text -

Quote:
> |
> |
> |
> |



Sat, 06 Sep 2003 10:08:20 GMT  
 How do you check is a registry key exist or not in VBScript/WSH?
Function KeyExists(ByVal RegKey)
    Dim Shell
    Dim ErrDesc(1)
    Set Shell = CreateObject("WScript.Shell")
    KeyExists = True
    On Error Resume Next
'===>  RegKey = Replace(RegKey & "\","\\","\")
' without the above line of code, the presence or absence of the final "\"
' is what determines whether you are checking for a key or a named value.
' It's then the caller's responsibility to pass the string with a final "\" to
' check for a key, or without a final "\" to check for a named value...
'===
    Shell.RegRead(vbcr)
    ErrDesc(0) = Replace(Err.Description,vbcr,"")
    Err.Clear
    Shell.RegRead(RegKey)
    ErrDesc(1) = Replace(Err.Description,RegKey,"")
    If ErrDesc(0) = ErrDesc(1) Then KeyExists = False
End Function

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--


Quote:
> OK,
> How do you find out if a Value Key exist or not?
> ("HKLM\Software\Microsoft\Windows NT\CurrentVersion\")

> Value Key = Currentversion



> > Try this function, passing it the key to be tested. The trailing backslash
> is optional.

> > Function KeyExists(ByVal RegKey)
> >     Dim Shell
> >     Dim ErrDesc(1)
> >     Set Shell = CreateObject("WScript.Shell")
> >     KeyExists = True
> >     On Error Resume Next
> >     RegKey = Replace(RegKey & "\","\\","\")
> >     Shell.RegRead(vbcr)
> >     ErrDesc(0) = Replace(Err.Description,vbcr,"")
> >     Err.Clear
> >     Shell.RegRead(RegKey)
> >     ErrDesc(1) = Replace(Err.Description,RegKey,"")
> >     If ErrDesc(0) = ErrDesc(1) Then KeyExists = False
> > End Function



> > | I need to check and see if a key exist or not and then
> > | do a procedure if true or false
> > |
> > | Key: ("HKLM\Software\Microsoft\Windows\CurrentVersion\Version")
> > | Key: ("HKLM\Software\Microsoft\Windows
> NT\CurrentVersion\CurrentVersion")
> > |
> > |
> > |
> > |



Sun, 07 Sep 2003 03:42:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. check if a certain registry key exists or not

2. Check if registry key exists

3. Check if registry key exists

4. Not running, am I doing something wrong

5. I want to send mail in WSH with JScript source...What am I doing wrong

6. I want to send mail in WSH with JScript source...What am I doing wrong

7. help please: example of seeing of a registry key exists

8. Detecing if a registry key exists

9. help please: example of seeing of a registry key exists

10. Doe a registry key exist?

11. Registry key exists?

12. How to determine if a Registry key exists?

 

 
Powered by phpBB® Forum Software