Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Environment variables ? 1

Status
Not open for further replies.

dkpede

Programmer
Dec 21, 2000
15
0
0
DK
Reading environment variables is all very well, but how do
I write them ?
Using the WSH I am calling an application using:
Code:
   set WshShell = WScript.CreateObject("WScript.Shell")
   WshShell.Run "Someprogram" , 1, TRUE
The problem is that this programs needs some Environment variables set in order to work...
I found the:
Code:
Set WshUsrEnv = WScript.Environment("User")
WshUsrEnv.Remove("EXAMPLE_1")
What I need is the corresponding:
Code:
WshUsrEnv.Add("MYVAR=Something")
But I can't find it..
 
You could use the WshShell.RegWrite method to

HKLM\System\CurrentControlSet\Control\Session Manager\Environment

or

HKCU\Environment

(These are NT/2000 settings)

Hope it helps, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Thanks Rob,

It was not quite what I needed as it also needs to run on win98. But your answer got me thinking about what is really going on, so I found another way of doing it, for anyone with a similar problem I have included an example:
Code:
   Set EnvObject = WshShell.Environment
   Original=EnvObject.Item("Tmp")
   EnvObject.Item("Tmp") = "c:\VbTmp"
   WScript.Echo "Tmp dir changed from " & Original & " To " & EnvObject.Item("Tmp")
   EnvObject.Item("MyVar") = "Something"
   WScript.Echo "MyVar created, value : " & EnvObject.Item("MyVar")

Thanks again,
Peter :)
 
I believe this only works in-process. It is not a permanent change. Run it, then go to a command prompt and type 'set'. On Win2K (don't know about 9x) it remained in its original state. The description for the .item property states it 'returns a specified item from a collection'.

-Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
You are right - it is only the Shell environment you change, however if you start other applications with .Run it is the changed environment that is used - which is just what I needed...

Happy Newyear,
Peter
 
Cool! I'm glad you found a solution (I'll probably need it someday).

later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top