My first attempt at Windows scripting, need some help

Jeff7181

Lifer
Aug 21, 2002
18,368
11
81
Ok... so the deal is that I'm trying to delete some registry keys and their subkeys but the keys vary from machine to machine. And I need to recreate one (or not delete one, just modify some DWords).

So... all the keys I'm trying to delete are in a key that's constant... lets say...

System\CurrentControlSet\Control\Video

And the keys might be...

System\CurrentControlSet\Control\Video\Key1
System\CurrentControlSet\Control\Video\Key2
System\CurrentControlSet\Control\Video\Key3
System\CurrentControlSet\Control\Video\Key4

I need to get rid of Key 1, 2, and 3 and modify the values within Key4's subkeys but they have subkeys so the only way I've been able to script that is to use a subroutine that calls itself. The problem with that is that it deletes all of them... which would be OK because I can recreate them as long as I can pull Key4's name from the registry and store it so it's recreated with the correct name, but I've been having a hard time doing that.

I think I'm probably going about this in a round about way... but here's the script I have so far that's working perfectly for deleting them:

Code:
Const HKCC = &H80000005

strComputer = "."
strKeyPath = "System\CurrentControlSet\Control\Video"

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

DeleteSubkeys HKCC, strKeypath
Sub DeleteSubkeys(HKCC, strKeypath)
	objRegistry.EnumKey HKCC, strKeyPath, arrSubkeys
		If IsArray(arrSubkeys) Then
		For Each strSubkey In arrSubkeys
			DeleteSubkeys HKCC, strKeyPath & "\" & strSubkey
		Next
	End If
	objRegistry.DeleteKey HKCC, strKeyPath
End Sub

Unfortunately, the Parent key is the last key to be deleted in that recursion, so the last value of strKeyPath is the parent key... and I want to recreate System\CurrentControlSet\Control\Video\Key4... which wouldn't be a problem if Key4's name was constant, but it's not.

Any suggestions?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,838
4,817
75
You're calling DeleteSubkeys HKCC, strKeypath with strKeypath="System\CurrentControlSet\Control\Video". So this code will delete "System\CurrentControlSet\Control\Video", as well as all subkeys. Perhaps you want to use objRegistry.EnumKey to get a list of the sub-keys and delete all but "Key4"? How do you plan to identify "Key4" anyway, if its name changes?

By the way, a subroutine that calls itself is called "recursion".
 

Jeff7181

Lifer
Aug 21, 2002
18,368
11
81
That's just it, I need the script to pull the name of that key. I know what its subkeys and values will be so I can recreate those... I just need to pull the name of the key and store it before it's deleted.

So... are you saying I should use objRegistry.EnumKey to pull all the subkey names and maybe store them in an array. Then maybe use a counter to delete all but 1? That could work...