Fairly newish to VBScripting. I keep getting a syntax error when I run this code below from within a sub routine.


Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const SECS_TO_WAIT = 20
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

Dim objWSHNetwork : Set objWSHNetwork = WScript.CreateObject("WScript.Network")
Dim oLocator : Set oLocator = CreateObject("Wbemscripting.SWbemLocator")

If KeyExists("HKLM\Software\PROGRAMNAME\") Then
'Registry Provider (StdRegProv) lives in root\default namespace.
Dim wmiNameSpace : Set wmiNameSpace = oLocator.ConnectServer(objWSHNetwork.ComputerName, "root\default")
Dim objRegistry : Set objRegistry = wmiNameSpace.Get("StdRegProv")
Dim sNames, sKeyName, sEnumPath, lRC

' Example Deletion of Value
Dim sPath : sPath = "SOFTWARE\PROGRAMNAME"

lRC = DeleteRegEntry(HKEY_LOCAL_MACHINE, sPath)

Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key. If it fails, start the subkey enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)

' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then

' Subkey Enumerator
On Error Resume Next

lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)

For Each sKeyName In sNames
If Err.Number <> 0 Then Exit For
lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
Next

On Error Goto 0

'At this point we should have looped through all subkeys, trying to delete the registry key again.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)

End If
End Function
End If

Function KeyExists(key)
On Error Resume Next
objWSHSHell.RegRead (key)
If Err = 0 Then KeyExists = True
End Function


The error that I receive refers this line of code:
Function DeleteRegEntry(sHive, sEnumPath)

Am I able to call functions from within a subroutine in vbscripting? That is the only assumption that I can make. This code works PERFECTLY on it's own, outside of a sub.
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
I tested your script by creating a dummy key PROGRAMNAME. I had to make small change to function KeyExists as object objWSHSHell wasnt created. The script worked without error.
Answered 11/14/2011 by: murali.bhat
Purple Belt

Please log in to comment
0
Why are you defining the function DeleteRegEntry inside an If loop?

End If
End Function
End If
this strikes me as very odd, just lift the function outside the loop.

PJ
Answered 11/15/2011 by: pjgeutjens
Red Belt

Please log in to comment
0
ORIGINAL: murali.bhat

I tested your script by creating a dummy key PROGRAMNAME. I had to make small change to function KeyExists as object objWSHSHell wasnt created. The script worked without error.


Oops, I forgot to copy that over from the main script. This is just a section of the main script. objWSHSHell was defined elsewhere.

I just tested the script alone too and it worked fine, but when added as a sub to my main script, I receive the error. So it works fine stand alone, but when it's made into a sub routine in my main script, it errors out. WTH?


ORIGINAL: pjgeutjens

Why are you defining the function DeleteRegEntry inside an If loop?

        End If
   End Function
End If
this strikes me as very odd, just lift the function outside the loop.

PJ


This is a snipet used from MS, only things I modified were to match what my varibles were named.
Answered 11/15/2011 by: mrwillya
Senior Yellow Belt

Please log in to comment
0
I'm pretty sure you cannot define functions inside subs or other functions (my scripting tool gives me a 'syntax error' too if I try it, I just checked)

Try taking the definition of the DeleteRegEntry (and any other functions) out from the sub and pasting them next to it, after the sub - end sub

PJ
Answered 11/15/2011 by: pjgeutjens
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity