Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

VBScript FAQ

General

Checking if Registry Key's or Value's exist
Posted: 11 May 05 (Edited 24 Jun 12)

Below are three functions which check
- If a registry KEY exists
- If a registry VALUE exists
- If a registry item exists (key or value, depending on what you pass it).

I found it very easy to check if a registry value existed, but checking if a registry key existed wasn't so easy. I found someone who had worked out a fix for this problem, so I cleaned it up a bit, and its preseted below for anyones reference :)

CODE

'************************************
'*
'* Registry Key Exists (Function)
'* Version 1.1
'*
'************************************
'This function checks to see if a passed registry key exists, and
'returns true if it does
'
'Requirements: The registry key you are looking for (RegistryKey)
Function RegistryKeyExists (fncStrRegistryKey)
'Ensure the last character is a backslash (\) - if it isn't, add one
If (Right(fncStrRegistryKey, 1) <> "\") Then
'Add a backslash
fncStrRegistryKey = fncStrRegistryKey & "\"
End If

'If there isnt the key when we read it, it will return an error, so we need to resume
On Error Resume Next

'Try reading the key
WshShell.RegRead fncStrRegistryKey

'Catch the error
Select Case Err
'Error Code 0 = 'success'
Case 0
fncStrRegistryKeyExists = true
'This checks for the (Default) value existing (but being blank); as well as key's not existing at all (same error code)
Case &h80070002
'Read the error description, removing the registry key from that description
strErrDescription = Replace(Err.description, fncStrRegistryKey, "")

'Clear the error
Err.clear

'Read in a registry entry we know doesn't exist (to create an error description for something that doesnt exist)
WshShell.RegRead "HKEY_ERROR\"

'The registry key exists if the error description from the HKEY_ERROR RegRead attempt doesn't match the error
'description from our fncStrRegistryKey RegRead attempt
If (strErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
RegistryKeyExists = true
Else
RegistryKeyExists = false
End If
'Any other error code is a failure code
Case Else
RegistryKeyExists = false
End Select

'Turn error reporting back on
On Error Goto 0
End Function


CODE

'************************************
'*
'* Registry Value Exists (Function)
'* Returns a value (true / false)
'*
'************************************
'This function checks to see if a passed registry value exists, and
'returns true if it does
'
'Requirements: The registry value you are looking for (RegistryValue)
Function RegistryValueExists (fncStrRegistryValue)
'Ensure the last character is NOT a backslash (\) - if it is, we aren't looking for a value
If (Right(fncStrRegistryValue, 1) = "\") Then
'It's not a registry value we are looking for
RegistryValueExists = false
Else
'If there isnt the value when we read it, it will return an error, so we need to resume
On Error Resume Next

'Try reading the value
WshShell.RegRead fncStrRegistryValue

'Catch the error
Select Case Err
Case 0:
'Error Code 0 = 'success'
RegistryValueExists = true
Case Else
'Any other error code is a failure code
RegistryValueExists = false
End Select

'Turn error reporting back on
On Error Goto 0
End If
End Function


CODE

'************************************
'*
'* Registry Item Exists (Function)
'* Version 1.1
'*
'************************************
'This function checks to see if a passed registry key/value exists, and
'returns true if it does
'
'Requirements: The registry key/value you are looking for (fncStrRegistryItem)
'Note: RegistryItem MUST end in a backslash (\) if you are looking for a key
' RegistryItem's without a backslash (\) will assume you are looking for a value
Function RegistryItemExists (fncStrRegistryItem)
'If there isnt the item when we read it, it will return an error, so we need to resume
On Error Resume Next

'Find out if we are looking for a key or a value
If (Right(fncStrRegistryItem, 1) = "\") Then
'It's a registry key we are looking for
'Try reading the key
WshShell.RegRead fncStrRegistryItem

'Catch the error
Select Case Err
'Error Code 0 = 'success'
Case 0:
RegistryItemExists = true
'This checks for the (Default) value existing (but being blank); as well as key's not existing at all (same error code)
Case &h80070002:
'Read the error description, removing the registry key from that description
ErrDescription = Replace(Err.description, fncStrRegistryItem, "")

'Clear the error
Err.clear

'Read in a registry entry we know doesn't exist (to create an error description for something that doesnt exist)
WshShell.RegRead "HKEY_ERROR\"

'The registry key exists if the error description from the HKEY_ERROR RegRead attempt doesn't match the error
'description from our RegistryKey RegRead attempt
If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
RegistryItemExists = true
Else
RegistryItemExists = false
End If
'Any other error code is a failure code
Case Else:
RegistryItemExists = false
End Select
Else
'It's a registry value we are looking for
'Try reading the value
WshShell.RegRead fncStrRegistryItem

'Catch the error
Select Case Err
Case 0:
'Error Code 0 = 'success'
RegistryItemExists = true
Case Else
'Any other error code is a failure code
RegistryItemExists = false
End Select
End If

'Turn error reporting back on
On Error Goto 0
End Function

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close