Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - %SystemRoot% being changed in script

Reply
 
Old 05-29-2009   #1 (permalink)
Tony Logan


 
 

%SystemRoot% being changed in script

Modified a script I found online that removes specified strings from the Path
environment variable. It works, but one thing it does that I don't get is
change "%SystemRoot%" to the hard-coded value of the variable
("C:\Windows\system32", for example).

Can anyone tell me why it does that, or how I can prevent it?

Here's the code:

Option Explicit
On Error Resume Next

Dim objReg, strComputer, strValueRight, strValueLeft
Dim strSearchFor, strKeyPath, strEntryName, strValue
Dim strTotalLength, strSearchForLength, strStartAt
dim strArraySearchFor

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strArraySearchFor=Array("C:\blah;")

' Connect to the registry (StdRegProv)
' Get the value for strValue (GetExpandedStringValue)
Set objReg=GetObject _
("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" & strComputer &
"\root\default:StdRegProv")

strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strEntryName = "path"
objReg.GetExpandedStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strEntryName,strValue

If (Right(strValue, 1) <> ";") Then
strValue=strValue+";"
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath,strEntryName, strValue
End if

For Each strSearchFor In strArraySearchFor
surgery
Next

Wscript.Quit(1)

Function surgery
' Calculates strStartAt which cuts the strSearchFor value from the path
strSearchForLength=Len(strSearchFor)
strStartAt = inStr(lCase(strValue), lCase(strSearchFor))

' Build the new path
' join the values to the left and right of strSearchFor
strTotalLength=Len(strValue)
strValueLeft=Left(strValue,(strStartAt-1))
strValueRight=Right(strValue,
(strtotalLength-strSearchForLength-(strStartAt-1)))
strValue=strValueLeft + strValueRight
' Reset the path in the registry (SetStringValue)
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath,strEntryName, strValue
End Function


My System SpecsSystem Spec
Old 05-29-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: %SystemRoot% being changed in script


"Tony Logan" <TonyLogan@xxxxxx> wrote in message
news:765B1BC4-0C27-41BE-B90A-6EA278601912@xxxxxx
Quote:

> Modified a script I found online that removes specified strings from the
> Path
> environment variable. It works, but one thing it does that I don't get is
> change "%SystemRoot%" to the hard-coded value of the variable
> ("C:\Windows\system32", for example).
>
> Can anyone tell me why it does that, or how I can prevent it?
>
This is by design: When you read a registry key of the type Reg_Expand_SZ
then all environmental variables are resolved. You will have to "unresolve"
them by obtaining the value x=%SystemRoot%, then performing this function:
path = replace(path, x, "%SystemRoot%", 1, -1, 1)
(untested)


My System SpecsSystem Spec
Old 05-29-2009   #3 (permalink)
Pegasus [MVP]


 
 

Re: %SystemRoot% being changed in script


"Tony Logan" <TonyLogan@xxxxxx> wrote in message
news:765B1BC4-0C27-41BE-B90A-6EA278601912@xxxxxx
Quote:

> Modified a script I found online that removes specified strings from the
> Path
> environment variable. It works, but one thing it does that I don't get is
> change "%SystemRoot%" to the hard-coded value of the variable
> ("C:\Windows\system32", for example).
>
> Can anyone tell me why it does that, or how I can prevent it?
By the way - having the statement "On error resume next" at the top of your
program is not nice. It makes trouble-shooting very difficult. Much better
to insert specific code that deals with errors, e.g. by checking for the
existence of a file *before* attempting to open it. Alternatively you could
cover individual blocks of code with an "On Error" statement and take
appropriate action when an error occurs (which you can determine by checking
err.number).


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Changed monitor also changed position of Window Mail Send/Receivewindow Vista mail
How to find %SystemRoot%\System32 folder Vista General
Re: VB Script Code to get alert when a folder get changed in intranet FTP Folder VB Script
Changed User name, but User folder name didnt get changed? Vista account administration
I need to change my %SystemRoot% Vista account administration


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46