Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for reg-key, if exist then delete 1

Status
Not open for further replies.

meneerB

IS-IT--Management
Oct 4, 2005
311
NL
hi all,
I don't have knowledge of vb at all, maybe someone can help:

I am using a vb script in which a registry key is deleted.
I found out that the script stopped with an error on that line, if the key already had been deleted/ does not exist.

what should i add to let VB first check if the key exists, if not, go on, of yes delete and go on.

thanks very much!
 
On Error Resume Next
strTemp = WshShell.RegRead("mykey.valueetc")
On Error Goto 0
If strTemp = "" Then
Msgbox "it doesnt exist"
Else
Msgbox "it does exist"
End If
'that might be very good as it could be there but just blank?

On Error Resume Next
Err.Clear
strTemp = WshShell.RegRead("mykey.valueetc")
If Err.Number <> 0 Then
Msgbox "its not there"
Else
Msgbox "its there"
End If
On Error Goto 0

' i was going to say, but in your case you can just wrap your deletion with On Error Resume Next and On Error Goto 0, this would seem to be a good idea...but it wouldnt catch the times where your script failed to delete the registry key for some reason, permissions or something...so i guess the second example above would be the best...though i do admit to using the 1st one most of the time.

so...

On Error Resume Next
Err.Clear
strTemp = WshShell.RegRead("mykey.valueetc")
If Err.Number <> 0 Then
Msgbox "its not there, no need to delete"
Else
Msgbox "its there"
Err.Clear
WshShell.RegDelete "your key" '????
If Err.Number <> 0 Then
Msgbox "failed to delete"
Else
Msgbox "deleted"
End If
End If
On Error Goto 0

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top