Jump to content

VBscript registry editing question


Idontwantspam

Recommended Posts

When using registry tools in VBscript, I've figured out how to read an existing value, write a value or delete a value, but I run in to a few problems when they value does not exist or the user does not have read (or write) permission for that key.

For example, if I want a script to see if HKCU\Software\Test DWORD value ThisIsATest (just a random entry) is set to 1 or 0, that's fine, I would do this:

Set WshShell = CreateObject( "WScript.Shell" )

WshShell.RegRead( "HKCU\Software\Testing\Test" )

But, if that key doesn't exist, then I get an error. Alternatively, say I go to delete that value.

Set WshShell = CreateObject( "WScript.Shell" )

WshShell.RegDelete( "HKCU\Software\Testing\Test" )

Which works fine, but if the value's not there, then I get an error.

Finally, let's say the user that the script is running as doesn't have permission to view that key. Then, no matter what I try to do, it gives an error.

So, what I'm wondering is if there's some way for the script to detect if the value even exists or not, and if the user has permission to view that key. If anyone has any sort of experience with this, I'd appreciate some help very much. :)

Link to comment
Share on other sites


You really need to use RegRead but look for error descriptions.

YourKey = "HKCU\Software\Testing\Test"


If Not RegKeyExists(YourKey) Then

WScript.Echo "Your key does not exist in the registry"

Else

WScript.Echo "Your key exists in the registry"

End If


Function RegKeyExists(sRegKey)

Dim oShell, RegReadReturn


Set oShell = CreateObject("WScript.Shell")


RegKeyExists = True

sRegKey = Trim (sRegKey)

If Not Right(sRegKey, 1) = "\" Then

sRegKey = sRegKey & "\"

End if

On Error Resume Next

RegReadReturn = oShell.RegRead(sRegKey)

If Err Then

If Left(err.description,7) = "Invalid" Then

'key not found...

RegKeyExists = False

'ElseIf Left(err.description,6) = "Unable" Then

'no default value set, but key exists...

'Else

'unexpected error

End if

Err.clear

End if

On Error Goto 0

End Function

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...