In DOS, if you need to create a series of directories, you can do this through the "MKDIR" command. For example, imagine that you’re working with a freshly formatted D: drive. You want to create the directory "MyFiles" with a subdirectory "SourceCode" which itself contains the subdirectory "VBScript". You issue the following command at a DOS prompt to do that:
mkdir D:\MyFiles\SourceCode\VBScript
Windows will create the "MyFiles" directory. It will then create the "SourceCode" directory inside that. Finally, it creates the "VBScript" directory inside the "SourceCode" directory.
Suppose you want to do the same thing within the Registry, creating a series of keys like the one illustrated below:
It’s possible that any of the keys in this particular hierarchy (SOFTWARE, Test, MyKeys, GoHere) do not exist. You want to be sure that they are all there, perhaps because you wish to write a value under the "GoHere" key.
The following code will allow you to do that. The following example would create the keys depicted in the screenshot above:
CreateRegPath "HKLM","SOFTWARE\Test\MyKeys\GoHere"
Function CreateRegPath(regHive,strRegPathToCreate)
Dim REG_HIVE
REG_HIVE = getRegHiveValue(regHive)
Dim pathParts, tempPath,i ,j
pathParts = Split(strRegPathToCreate,"\",-1)
For i=0 To UBound(pathParts)
tempPath = ""
If i > 0 Then
For j=0 To i
tempPath = tempPath & pathParts(j) & "\"
Next
If RegistryEntryExists(reghive & "\" & tempPath)=False Then
CreateRegKey regHive,tempPath
End If
ElseIf i=0 Then
If RegistryEntryExists(regHive & "\" & pathParts(0))=False Then
CreateRegKey regHive, pathParts(0)
End if
End if
Next
End Function
Function CreateRegKey(regHive,regKeyPath)
Dim REG_HIVE
REG_HIVE = getRegHiveValue(regHive)
If regKeyPath<>"" Then
Dim oReg
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")
RC = oReg.CreateKey(REG_HIVE,regKeyPath)
End if
End Function
Function RegistryEntryExists(theEntry)
On Error Resume Next
Dim strEntry, oShell
Set oShell = CreateObject("WScript.Shell")
strEntry = oShell.RegRead(theEntry)
If Err.Number <> 0 Then
Err.Clear
RegistryEntryExists = False
Else
Err.Clear
RegistryEntryExists = True
End If
End Function
Function getRegHiveValue(strRegHive)
strRegHive = UCase(strRegHive)
Dim regHive, oReg
Select Case strRegHive:
Case "HKCR","HKEY_CLASSES_ROOT":
regHive = &H80000000
Case "HKCU","HKEY_CURRENT_USER":
regHive = &H80000001
Case "HKLM","HKEY_LOCAL_MACHINE":
regHive = &H80000002
Case "HKU","HKEY_USERS":
regHive = &H80000003
Case "HKCC","HKEY_CURRENT_CONFIG":
regHive = &H80000005
Case "HKDD","HKEY_DYN_DATA":
regHive = &H80000006
Case Else:
WScript.Echo "==================================="
WScript.Echo "Requested Registry Hive Value for: " & strRegHive
WScript.Echo "Hive ‘" & strRegHive & "’ is not recognized."
WScript.Echo "==================================="
WScript.Quit 13
End Select
getRegHiveValue = regHive
End Function
The above code contains four separate functions you may find useful.
- RegistryEntryExists(): Given a string that contains the entire path to a registry key or value, this function will return True if that key or value exists, and false if it does not.
- GetRegHiveValue(): Given a string that represents one of the major registry hives (e.g., "HKLM", "HKEY_CLASSES_ROOT"), this function returns the hex value used by the WMI registry functions to access that registry hive.
- CreateRegKey(): Given a string representing the registry hive you want to modify and a path to a registry key (folder) you want to create, this function will create that key (provided all keys above it in the path exist).
- CreateRegPath(): Given a string representing the registry hive to be modified and a full path to a registry key, this function will produce all the necessary registry keys to ensure that the desired path exists. This works the same way the DOS "mkdir" command does for folders on the hard disk. It requires the three functions above in order to work.