|
Several months ago I was searching for a way to add environmental variables to a system via a vbscript because we have a piece of software which requires special paths set in order to run. I was unable to find anything at the time which was able to do what I wanted it to do. Well today while browsing the forums over at http://petri.co.il I stumbled across a script posted by Rems which does exactly what I needed. Since it was so hard to find before, I decided to post it here... hoping the next person doesn't have to look so hard.
Open up notepad or your favorite text editor Copy and paste the following text ----------COPY EVERYTHING BELOW THIS LINE---------- ' This vbscript includes a Sub routine for ADDing ' value(s) to an environment variable. ' (OPTIONAL, it'd also be possible to ' include an other Sub routine to be able also to delete values) ' http://www.petri.co.il/forums/showthread.php?t=40514' ' Posted by Username Rems Const ENVIRONMENT_VAR = "PATH" COnst ENVIRONMENT_TYPE = "USER" Dim WSHShell, sEnvVar, arrAddItems sEnvVar = "%" & ENVIRONMENT_VAR & "%" Set WSHShell = WScript.CreateObject("WScript.Shell") ' Item to add to the environment. Wrap each item between quotes ' (note, Multiple items should be COMMA-separated in the array!) arrAddItems = array("c:\temp") AddToEnv arrAddItems Sub AddToEnv(AddItems) Dim colEnvVars, strVarExpandedFull, arrVarExpandedFull Dim strEnvCurrentValues, sAddItem, sCurItem, CompA, CompB Dim iVal, PrintableChars, x, i, c, sep, sNewItems sep= Empty : sNewItems = Empty With WSHShell Set colEnvVars = .Environment(ENVIRONMENT_TYPE) strVarExpandedFull = .ExpandEnvironmentStrings(sEnvVar) arrVarExpandedFull = Split(strVarExpandedFull, ";") For x = 0 To Ubound(AddItems) iVal= FALSE sAddItem = Trim(AddItems(x)) CompB = LCase(.ExpandEnvironmentStrings(sAddItem)) CompB = Replace(Replace(CompB, "/", "\"), chr(34), "") If InStrRev(CompB, "\") = Len(CompB) _ Then CompB = Left(CompB, Len(CompB)-1) For i = 0 To Ubound(arrVarExpandedFull) sCurItem = Trim(arrVarExpandedFull(i)) If Len(sCurItem) >0 Then CompA = LCase(.ExpandEnvironmentStrings(sCurItem)) CompA = Replace(Replace(CompA, "/", "\"), chr(34), "") If InStrRev(CompA, "\") = Len(CompA) _ Then CompA = Left(CompA, Len(CompA)-1) If CompB = CompA Then iVal= TRUE End If Next If iVal= FALSE Then sNewItems = sNewItems & sep & sAddItem Sep = ";" End If Next End With If Len(sNewItems) >0 Then strEnvCurrentValues = colEnvVars(ENVIRONMENT_VAR) PrintableChars = False For c = 32 to 126 If inStr(strEnvCurrentValues, chr(c)) _ then PrintableChars = True Next If sEnvVar strEnvCurrentValues Then If PrintableChars = True Then colEnvVars(ENVIRONMENT_VAR) = strEnvCurrentValues & ";" & sNewItems Else colEnvVars(ENVIRONMENT_VAR) = sNewItems End If Else colEnvVars(ENVIRONMENT_VAR) = sNewItems End If End If End Sub Sub RemoveFromVar(DelItems) With WSHShell Set colEnvVars = .Environment(ENVIRONMENT_TYPE) arrEnvCurrentValues = Split(colEnvVars(ENVIRONMENT_VAR), ";") Dim sep: sep = Empty For i = 0 To Ubound(arrEnvCurrentValues) sCurItem = Trim(arrEnvCurrentValues(i)) If Len(sCurItem) >0 Then CompA = LCase(.ExpandEnvironmentStrings(sCurItem)) CompA = Replace(Replace(CompA, "/", "\"), chr(34), "") If InStrRev(CompA, "\") = Len(CompA) _ Then CompA = Left(CompA, Len(CompA)-1) iVal = TRUE For x = 0 To Ubound(DelItems) sDelItem = Trim(DelItems(x)) CompB = LCase(.ExpandEnvironmentStrings(sDelItem)) CompB = Replace(Replace(CompB, "/", "\"), chr(34), "") If InStrRev(CompB, "\") = Len(CompB) _ Then CompB = Left(CompB, Len(CompB)-1) If CompB = CompA Then iVal = FALSE rem wscript.echo CompB End If Next If iVal = TRUE Then sNewItems = sNewItems & sep & sCurItem Sep = ";" End If End If Next End With colEnvVars(ENVIRONMENT_VAR) = sNewItems End Sub ----------COPY EVERYTHING ABOVE THIS LINE---------- Make sure there is no word wrapping happening anywhere. Edit the array items to suite your needs and save as SetEnvVarPath.vbs Now you can run this as a logon script for the user to set the paths automatically. THIS INFORMATION IS PROVICED "AS IS" WITH NO WARRANTIES EXPRESSED OR IMPLIED
|