Bill said:
Hi Mark,
I like your idea of detecting the path elements as separate pieces, rather
than as a single contiguous unit. I might get talked into doing this if you
or others see a lot of cases where it is necessary.
Hi
I have created a vbscript program (posted below) I use to check if
the path is basically ok, (type REG_EXPAND_SZ + checking for the
3 standard path elements as separate pieces).
It also sets the process environment correctly if an "error" was
detected, so the rest of the script will run with a correct path
(the script is part of a much bigger script that "wraps" software
installations).
'--------------------8<----------------------
Option Explicit
Dim oShell, oWshProcessEnv
Set oShell = CreateObject("WScript.Shell")
Set oWshProcessEnv = oShell.Environment("Process")
' Check if system path is of type REG_EXPAND_SZ
CheckSystemPathRegType
' Checking if correct path to system folders are in place,
' and if not, add them
CheckSystemPathEntry("%SystemRoot%\System32\Wbem")
CheckSystemPathEntry("%SystemRoot%\System32")
CheckSystemPathEntry("%SystemRoot%")
Sub CheckSystemPathRegType
Dim sProcessPath, sRegPath
sProcessPath = oWshProcessEnv("PATH")
If InStr(sProcessPath, "%") > 0 Then
' system path is most likely of type REG_SZ, needs to be REG_EXPAND_SZ
On Error Resume Next
sRegPath = ""
sRegPath = oShell.RegRead _
("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
If sRegPath <> "" Then
oShell.RegWrite _
"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", _
sRegPath, "REG_EXPAND_SZ"
End If
On Error Goto 0
oWshProcessEnv("PATH") = oShell.ExpandEnvironmentStrings _
(oShell.ExpandEnvironmentStrings("%PATH%"))
End If
End Sub
Sub CheckSystemPathEntry(ByVal sSubPath)
Dim sRegPath, sSubPathExpanded, sExpRegPath, sExpEnvPath
sSubPathExpanded = LCase(oShell.ExpandEnvironmentStrings(sSubPath))
On Error Resume Next
sRegPath = ""
sRegPath = oShell.RegRead _
("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
sExpRegPath = LCase(oShell.ExpandEnvironmentStrings(sRegPath))
On Error Goto 0
If Right(sExpRegPath, Len(sSubPathExpanded)) = sSubPathExpanded Then
Exit Sub ' ---->
End if
If InStr(sExpRegPath, sSubPathExpanded & ";") = 0 Then
On Error Resume Next
sRegPath = sSubPath & ";" & sRegPath
oShell.RegWrite _
"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", _
sRegPath, "REG_EXPAND_SZ"
On Error Goto 0
End If
sExpEnvPath = LCase(oShell.ExpandEnvironmentStrings("%PATH%"))
If InStr(sExpEnvPath, sSubPathExpanded & ";") = 0 Then
On Error Resume Next
sRegPath = sSubPath & ";" & sRegPath
oWshProcessEnv("PATH") = sSubPathExpanded & ";" & sExpEnvPath
On Error Goto 0
End If
End Sub
'--------------------8<----------------------