xka
xka

Reputation: 63

Delete Registry Key with spaces at the end of line through VBS

It seems that reg delete does not work with keys that have spaces at the end. I'm trying to delete a key with spaces, for example:

"HKCU\Software\Microsoft\Sample "

Does anyone have any idea on how to delete this, I already use the replace and trim function to convert the registry without space, but unfortunately I can't get it to work.

RegDeleteKey "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\samplekey ", "TMPUSER"

Function SetKeys(SID)
    RegDeleteKey "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\sampleykey ", SID
End Function

Sub RegDeleteKey(KeyPath, Mode)
    Dim CMD

    If Mode = "TMPUSER" Then
        KeyPath = "TMPUSER\" & KeyPath
    Else
        KeyPath = Mode & "\" & KeyPath
    End if

    CMD = "REG DELETE " & chr(34) & "HKEY_USERS\" & KeyPath & chr(34) & " /f"

    objshell.Run CMD, 0, True   
End Sub

Upvotes: 2

Views: 1308

Answers (1)

Tomalak
Tomalak

Reputation: 338278

No need to use reg.exe at all. The WshShell object has a RegDelete method. The docs say:

Specify a key-name by ending strName with a final backslash; leave it off to specify a value-name.

So...

Dim Shell: Set Shell = CreateObject("WScript.Shell")


If TryRegDelete("HKCU\Software\Microsoft\Sample \") Then
    WScript.Echo "Success!"
Else
    WScript.Echo "Could not delete key."
End If

Function TryRegDelete(strName)
    On Error Resume Next
    Shell.RegDelete(strName)

    TryRegDelete = Err.Number = 0
    On Error GoTo 0
End Function

Upvotes: 4

Related Questions