Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







WSH Forum

Author Thread: Set Enviroment Variables
4scriptomini
Set Enviroment Variables
Posted: Friday, May 04, 2007 1:32 AM (PST)
I am having a hard time. Maybe someone can help me out. I need to set a envirmoent variable and retrive by a cmd using %variable%. yes i have googled a lot... the code: Set objShell = WScript.CreateObject("WScript.Shell") Set colUsrEnvVars = objShell.Environment("PROCESS") colUsrEnvVars("TEMP") = "Upgraded" Wscript.Echo ObjShell.ExpandEnvironmentStrings("%TEMP%") then I go to cmd and and type echo %temp% and IT IS NOT the value I set!!!! HELP MEEEeeeee


Comments:

Author Thread:
Daren
Set Enviroment Variables
Posted: Monday, May 07, 2007 9:05 AM (PST)


4scriptomini,

 

The environment variables have a couple of minor gotcha's.

 

1. Don't use "TEMP" as your variable as it is used by the system
to point to the temporary directory used by windows and apps.


2. If you want to use environment variables that are read
by other applications, you should not use "PROCESS", but
instead (User, System, or Volatile).

 

The reason is that "PROCESS" variables only exist for the
process they are running in, but may be passed to child
processes.

 

"USER" -> Applies to current user, saved between logoffs and restarts.

 

"SYSTEM" -> Applies to all users, saved between logoffs and restarts.

 

"VOLATILE" -> Applies to current logon session and NOT saved between
logoffs and restarts.

 

3. A quirk of windows is that when you open a command shell (DOS Prompt)
it reads the various environment variables (once).  If I run a script
that sets an environment variable you will have to open another shell
and type "SET" to verify that it was actually set. 

 

 

4. I successfully ran the follow on Windows XP.

 

var shell = WScript.CreateObject( "WScript.Shell" );
var volatile = shell.Environment( "VOLATILE" );
volatile( "MyVariable" ) = "Some Value";

 

 

Open a new shell, and type "SET" (no quotes) and you should see it.

 

Hope it helps,

Daren

nicephotog
Set Enviroment Variables
Posted: Thursday, August 23, 2007 11:07 PM (PST)
With .bat batch scripting there is also a command called setlocal and its endlocal. When you use it the environment is set for the duration of the script regardless of the actual settings for the same inside the configuration file for the OS. Maybe thats the problem with the execution of the script somewhere. Also that section of DOS to windows command scripting system that goes through the CMD or command interpretor has differing abilities and syntax. 98 and Me do not have CMD, NT4 is different to them and Later NT5 and now NT6(Vista) are more advanced in the syntax of batch to interpretor or DOS to interpretor, that finally is Windows mixture to interpretor. Here are the keywords i typed into yahoo, its yielding ok for the types of things it brought back. batch dos .bat syntax local set environment



WSH and ADSI Administrative Scripting


Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon