Monday, March 24, 2014

VB Script to work with windows environment variables

Some applications require that a folder be added to the path environment variable to find DLL's or configuration Files. this is as simple as Calling the path, then Appending a ; and thenewpath to it.

 Set ObjWsh = WScript.CreateObject("WScript.Shell")   
 Set WshEnv = objWsh.Environment("SYSTEM")   
 WshEnv("Path") = WshEnv("Path") & ";C:\PathTo\Add"  

there are 2 types of environment variables we can use - User and System. User apply only to the current user, Whereas System are system wide. the Line WshEnv=objWsh.Environment("SYSTEM") creates the WSHEnv Object set to system - so when we call the Path variable in the next line, it is the system path, and not the user path we are returning.

In this example, to append a folder to the path, we set the Path WshEnv("Path") equal to itself + the & (amperstand concatenation string) and the string we want to add (Prefixed with a semicolon, which is the Separator used in the path Variable) ";C:\PathTo\Add" (N.B to enclose this in Quotes since its a literal string)

No comments:

Post a Comment