Delete part of the SYSTEM Path Environment Variable

Delete part of the SYSTEM Path Environment Variable

Post by RGlnaW1hbj » Tue, 22 Feb 2005 03:15:02


Hi,
I am trying to delete part of the Path statement in evironment variables on
XP and 2000 boxes. If for example, I have a machine that has the value
C:\temp1 included in the SYSTEM path statement, I would like to delete it
without affecting the rest of the Evironment line. I know the best way is to
Read the Registry under
SYSTEM\CurrentControlSet\Control\Session Manager\Environment (PATH)
and then to do a compare....change the text and pop it back in.
Any scripting Ideas. After I try a script for 2 days and and search
Google..etc...until my eyes are read....I usually give in.
Thanks for any input.
Jeff L
 
 
 

Delete part of the SYSTEM Path Environment Variable

Post by MV » Tue, 22 Feb 2005 06:43:03


There are 2 PATH variables, one in the USER environment and one on the
SYSTEM environment...

Here's sample script for how I would do this...

option explicit

call removePathEntry("user","") 'pass the entry to remove as the 2nd
argument
call removePathEntry("system","")

sub removePathEntry(argEnv,argEntry)

dim shell,env,path,arPathEntries,n,entry

wscript.echo "argEnv:",argEnv
wscript.echo "argEntry:",argEntry
set shell = createobject("wscript.shell")
set env = shell.environment(argEnv)
path = env("path")
wscript.echo argEnv,"before:",path
arPathEntries = split(path,";")
for n = 0 to ubound(arPathEntries)
entry = arPathEntries(n)
if lcase(entry) = lcase(argEntry) then
arPathEntries(n) = ""
elseif trim(entry) <> "" then
arPathEntries(n) = entry & ";"
else
arPathEntries(n) = ""
end if
next
path = join(arPathEntries,"")
wscript.echo argEnv," after:",path
env("path") = path

end sub



--
Michael Harris
Microsoft MVP Scripting
http://www.yqcomputer.com/ %20WA%20US